Standards for the new web.
a way to define and use custom HTML elementsDefinition
var CustomElementPrototype = Object.create(HTMLElement.prototype);
CustomElementPrototype.property = value;
CustomElementPrototype.method = function(){
// ...
};
CustomElementPrototype.createdCallback = function() { /* ... */ };
CustomElementPrototype.attachedCallback = function() { /* ... */ };
CustomElementPrototype.attributeChangedCallback = function(attrName, oldVal, newVal) { /* ... */ };
var CustomElement = document.registerElement('custom-element', {
prototype: CustomElementPrototype
});
Usage (JavaSCript)
var customElement = document.querySelector('#customElement');
// or
var customElement = document.createElement('custom-element');
document.body.appendChild(customElement);
// or
var customElement = new CustomElement();
document.body.appendChild(customElement);
customElement.item = "...";
customElement.addEventListener('event', function(e) {
// process event here
});
not really a module system .. just loading stuff
encapsulating the DOM!
var shadow = document.querySelector('#container').createShadowRoot();
var template = document.querySelector('#template');
var clone = document.importNode(template.content, true);
shadow.appendChild(clone);
more like and inert container than a template.
Everything is an element!
core-elements
paper-elements
(implements Material design)DOM is the module system.
* either Polymer or plain native WebComponent
Custom element area just HTML elements!Definition
{{profile.name}}
{{profile.title}}
Usage
No data!
- {{item.label}}
PolymerExpressions.prototype.myFilter = function( input, arg1, arg2, ...) {
// Transformation ...
return output;
}
Definition (in element)
Polymer({
// ...
myFilter: function( input, arg1, arg2, ...) { /* ... */ }
// ...
})
Usage
{{ expression | myFilter }}
{{ expression | myFilter:arg1:arg2:... }}
this.fire('event', {
// event data here
})
Catch Event (HTML)
Catch Event (JavaScript)
customElement.addEventListener('event', function(e){
e.stopPropagation();
// handle event ...
});
Polymer({
usernameChanged: function validateUsername(){
// validation code here ...
}
});
"Advanced" Property Change Listener
Polymer({
observe: {
usernameInput: 'validateUsername'
},
validateUsername: function validateUsername(){
// validation code here ...
}
});
Attribute Change Listener
Polymer({
attributeChanged: function (attribute, oldValue, newValue){
// validation code here ...
}
});
Bad news!
There is no routing in Polymer.
Wait! That's not a bad news!
app-router ...
{{item.name}}
{{item.title}}