Marzhill Musings


First Look at Polymer Elements

Published On: 2013-09-17


For work I've been getting up to speed on the W3C's set of webcomponents standards. Which means I've been looking at Polymer. Polymer is both an experimental javascript framework and a shim library that simulates the portions of the W3C standard which are not yet implemented in browsers. Specifically I've been looking at Custom Elements and Templates since they are the more concrete portions of the standard right now.

At some point when you are exploring a new technology the docs and tutorials stop being useful to you and to really get a feel you have to build something in it. I decided to port parts of a javascript application, DynamicBible, that I had already written as a learning exercise. DynamicBible currently uses requirejs for it's javascript code as a way to manage dependencies and keep scoping sane. This made it perfect for my purposes since it allowed me to explore two types of polymer elements.

  • UI elements.
  • Utility elements that don't have a visible presence on the page.

For my purposes I ported the DynamicBible search box and a requirejs importer element. In this article I'll cover the search box element. The requirejs element will be covered a later article.

Creating your own Polymer Element

    1 <polymer-element name="my-element">
    2 <template>
    3 ...
    4 </template>
    5 <script>
    6 ...
    7 </script>
    8 </polymer-element>

The <polymer-element> is the declarative way to define your polymer element in html itself. The bare minimum to define a polymer element would be

    1 <polymer-element name="search-box">
    2 <script>
    3 Polymer('search-box');
    4 </script>
    5 </polymer-element>

This is as about as useful as the span element though and html already has a span element. We need a little more than this to be worth it. Our element needs some attributes and behavior. Polymer lets us describe the expected attributes using an attribute called, what else, attributes.

    1 <polymer-element name="search-box" attributes="id query">

As for the behavior to attach to this element, that brings us the the Polymer construction function.

    1 <polymer-element name="search-box" attributes="id query">
    2 <script>
    3 // Polymers construction function.
    4 Polymer('search-box', {
    5   // default values for the attributes
    6   id: "",
    7   query: "",
    8   // a search method as part of the search-boxes api.
    9   search: function() {
   10     submitSearch(this.value);
   11   }
   12 });
   13 </script>
   14 </polymer-element>

You can use the element the same way you would any other html element.

    1 <search-box id="searchBox"></search-box>

Now our element has a method on it that will submit a search using the value of our search-box query attribute. We could trigger this behavior right now with javascript.

    1 document.querySelector('#searchBox').query = "what is a polymer element?";
    2 document.querySelector('#searchBox').search();

It's kind of silly that we have to do that manually with javascript though. What we really want is for this element to detect changes in our query and perform the search for us.

    1 <polymer-element name="search-box" attributes="id query">
    2 <script>
    3 Polymer('search-box', {
    4   id: "",
    5   query: "",
    6   search: function() {
    7     submitSearch(this.value);
    8   },
    9   // event handlers
   10   queryChanged: function() { // called on query attribute change
   12   },
   13   created: function() { // called on element creation
   14   }
   15 });
   16 </script>
   17 </polymer-element>

Now when the element's query attribute changes a search is triggered.

Up to now our element hasn't been very visible. We need to give it an image boost. We can do this two different ways.

  1. Using a template
  2. Using element inheritance

We'll go with the template element for now. The element inheritance will come in handy later.

    1 <polymer-element name="search-box" attributes="id query">
    2 <template>
    3   <input type="text" value="{{ query }}">
    4 </template>
    5 <script>
    6 Polymer('search-box', {
    7   id: "",
    8   query: "",
    9   search: function() {
   10     submitSearch(this.value);
   11   },
   12   // event handlers
   13   queryChanged: function() { // called on query attribute change
   15   },
   16   created: function() { // called on element creation
   17   }
   18 });
   19 </script>
   20 </polymer-element>

There are a number of things going on in this template element. First we define some html that will be used whenever we render the element on a page. There is some complicated merging logic involving the shadow dom but we'll ignore that for now. Second our value attribute on the the input element has funny looking content. The {{ query }} tells polymer that the contents of the text box and the query a should be kept in sync. A change to one of them is reflected in the other. Furthermore a change to the input box will result in the queryChanged event firing and causing a search to get submitted. There are several more built in events and Polymer includes a way to create and listen to your own events. as well.

I'll cover a utility element that shims requirejs modules to make them useable in your polymer elements in a later article.

Out elements template element isn't terribly complicated and it turns out in our case is completely unnecessary. We can use extends to tell Polymer our element should inherit from the input element.

Our last tweak to the search-box element looks like this.

    1 // input already defines the attributes we need
    2 <polymer-element name="search-box" extends="input">
    3 <script>
    4 Polymer('search-box', {
    5   id: "",
    6   // query becomes the value attribute of an input element.
    7   value: "",
    8   search: function() {
    9     submitSearch(this.query);
   10   },
   11   // event handlers
   12   valueChanged: function() { // called on value attribute change
   14   },
   15   created: function() {
   16     // Initialize the type to text on element creation
   17     this.type = "text"
   18   }
   19 });
   20 </script>
   21 </polymer-element>