Repository navigation
Your Component & Polymer
Appmaker components are built with the help of a framework called Polymer. It's a neat framework that helps you built your own custom HTML elements. In this guide, we'll show you the basics of what you need to know about Polymer! We'll be using the Counter Component from the Getting Started repo.
You can jump to the full Polymer docs, or follow along!
####We'll take a look at...
Polymer has some cool tricks for handling attributes. Our counter element has three attributes: unit, increment, and value.
<polymer-element name="ceci-counter" extends="ceci-element" attributes="unit increment value">
####Getting or setting an attribute
To set an attribute value, we can include the default value directly in the element tag like this.
<polymer-element name="ceci-counter" extends="ceci-element" attributes="unit increment value" value="5">
We can also set it in the Polymer definition...
value : 0,
Or use the this.attributeName shorthand inside of a method in our element.
this.value = 0;
We can also use the classic standby...
this.setAttribute("value","0")
####Tracking changes When an attribute changes, Polymer will automatically look for and fire an attributeChanged function with the corresponding name. Here's how the counter component handles changes to the increment attribute.
incrementChanged: function(oldValue, newValue) {
this.increment = Number(this.increment) || 0;
}
Polymer allows you bind special template variables in of your element's markup to it's attributes, properties and methods. The counter component uses this data binding to display the value attribute and the displayUnit property, like this...
<div class="count-wrapper">
<span class="count">{{value}}</span>
<span class="count-label">{{displayUnit}}</span>
</div>
So when the count changes, the element will be automatically updated.
Polymer provides a shorthand for binding common events to methods inside an element as well. The following demonstrates using a click handler to fire off a method. Notice that you can use the data binding stuff from above to refer to methods as well!
<button on-click="{{clickHandler}}" />
clickHandler : function(){ console.log("I done been clicked!") }
You can use this shorthand for all common web events like:
- keypress
- mouseenter
- And many more!
###Selecting Nodes Polymer has some cool shorthand for selecting nodes with an id attribute. You can select a node with id="jumbo" using...
this.$.jumbo
For other types of selectors, it's best to use the shadowRoot combined with a querySelector. So, for selecting a node with class="important"
this.$.shadowRoot.querySelector(".important")