Polymer - Data System

Polymer allows observing the changes on an element's properties by taking different actions such as −

  • Observers − It invokes the callbacks whenever the data changes.

  • Computed Properties − It computes the virtual properties based on other properties, and re-computes them whenever the input data changes.

  • Data Bindings − It updates the properties, attributes, or the text content of a DOM node using annotations whenever the data changes.

Data Paths

Path is a string in the data system, that provides a property or a sub-property relative to a scope. The scope can be a host element. Paths can be linked to different elements using data binding. Data change can be moved from one element to another, if the elements are connected with data binding.


<dom-module id = "my-profile">
      . . .
      <address-card address="{{myAddress}}"></address-card>
   . . .

The above two paths (my-profile and address-card) can be connected with data binding, if <address-card> is in the local DOM of the <my-profile> element.

Following are the special types of path segments in Polymer.js −

  • The wild card (*) character can be used as the last segment in a path.

  • The array mutations can be displayed to a given array by placing string splices as the last segment in a path.

  • Array item paths indicate an item in an array and the numeric path segment specifies an array index.

In a data path, each path segment is a property name and they include following two kinds of paths −

  • The path segments separated by dots. For example: "apple.grapes.orange".

  • In an array of strings, each array element is either a path segment or a dotted path. For example: ["apple","grapes","orange"], ["apple.grapes","orange"].

Data Flow


The following example specifies a two-way binding of data flow. Create an index.html file and add the following code in it.

<!doctype html>
      <title>Polymer Example</title>
      <script src = "bower_components/webcomponentsjs/webcomponents-lite.js"></script>
      <link rel = "import" href = "bower_components/polymer/polymer.html">
      <link rel = "import" href = "my-element.html">

Now create another file called my-element.html and include the following code.

<link rel = "import" href = "bower_components/polymer/polymer-element.html">
<link rel = "import" href = "prop-element.html">

//it specifies the start of an element's local DOM
<dom-module id = "my-element">
      <prop-element my-prop="{{demoProp}}"></prop-element>
         Present value: <span>{{demoProp}}</span>
      Polymer ({
         is: "my-element", properties: {
            demoProp: String

Next, create one more file called prop-element.html and add the following code.

//it specifies the start of an element's local DOM
<dom-module id = "prop-element">
      <button on-click = "onClickFunc">Change value</button>
      Polymer ({
         is: "prop-element", properties: {
            myProp: {
               type: String,
               notify: true,
               readOnly: true,
               value: 'This is initial value...'
         onClickFunc: function(){
            this._setMyProp('This is new value after clicking the button...');


Run the application as shown in the previous chapters, and navigate to Following will be the output.

Polymer Data Flow Example

After clicking the button, it will change the value as shown in the following screenshot.

Polymer Data Flow Example

Linking Two Paths

You can link the two paths to the same object using the linkPaths method and need to use data binding to generate changes between the elements.


linkPaths('myTeam', 'players.5');

The path linkage can be removed using the unlinkPaths method as shown below −



The observable changes that occur to the element's data invoke methods known as observers. Following are the types of observers.

  • Simple observers are used to observe a single property.

  • Complex observers are used to observe more than one property or path.

Data Binding

Data binding can be used to connect the property or an attribute of an element from the host element in its local DOM. Data binding can be created by adding annotations to DOM template as shown in the following code.

<dom-module id = "myhost-element">
      <target-element target-property = "{{myhostProperty}}"></target-element>

The anatomy of data binding in the local DOM template looks like the following −




The left-hand side of the binding specifies the target property or attribute, while the righthand side of the binding specifies either a binding annotation or a compound binding. The text in binding annotation are enclosed by double curly bracket ({{ }}) or double square bracket ([[ ]]) delimiters and the compound binding includes one or more string literal binding annotations.

Following are the helper elements, which are used with data binding use cases −

  • Template Repeater − An instance of the template's contents can be created for each item in an array.

  • Array Selector − It provides the selection state for an array of structured data.

  • Conditional Template − You can identify the content, if the condition is true.

  • Auto-binding Template − It specifies the data binding outside of polymer element.

The DOM tree triggers a dom-change event, if the helper elements update the DOM tree. Sometimes, you can interact with DOM by changing the model data, not by interacting with the created nodes. Therefore, you can use the dom-change event to access the nodes directly.