Binding Model Data in a PolymerTemplate

At the core of PolymerTemplate is the way model values are bound to different parts of the element tree defined by the template. To get started with using templates and learn how to set model values, see Creating A Simple Component Using the Template API.

Binding Text Content

The value of a model property can be used as the text content of an element using [[propertyName]] inside a tag.

Sample HTML Template

  1. <dom-module id="my-template">
  2. <template>
  3. <div>[[hostProperty]]</div>
  4. </template>
  5. <script>
  6. class MyTemplate extends Polymer.Element {
  7. static get is() {return 'my-template'}
  8. }
  9. customElements.define(MyTemplate.is, MyTemplate);
  10. </script>
  11. </dom-module>

For the server-side sample see: Server-side model sample code

Binding Property Values

You can set an element property value based on a model by using the property name in attribute form (dash-case not camelCase):

HTML

  1. <dom-module id="my-template">
  2. <template>
  3. <my-element my-property="[[hostProperty]]"></my-element>
  4. </template>
  5. ...
  6. </dom-module>

This example binds to the target property, myProperty on <my-element>.

Note
name=”[[binding]]” defines that the element property named name should get it’s value from the model property named binding, whereas name=”binding” (i.e. without brackets) defines that the element attribute named name should have the value binding regardless of any value in the model.

There are a handful of common native element properties that Polymer can’t data-bind to directly, because the binding causes issues on one or more browsers and you need to use attribute bindings instead. For more information on these properties see: Native properties that don’t support property binding

Binding Attribute Values

A binding written as <div something="[[hostProperty]]"></div> is bound to the property something because the property can typically be changed on the fly while the attribute is often used only for the initial value.

When you want to explicitly bind to an attribute instead, use the attribute name followed by $:

HTML

  1. <dom-module id="my-template">
  2. <template>
  3. <div something$="[[hostProperty]]"></div>
  4. </template>
  5. ...
  6. </dom-module>

or

HTML

  1. <dom-module id="my-template">
  2. <template>
  3. <a href$="[[hostProperty]]"></a>
  4. </template>
  5. ...
  6. </dom-module>
Note

Text surrounded by double curly bracket {{ }} or double square bracket [[ ]] delimiters identify the host data being bound.

  • Double-curly brackets support both upward and downward data flow. see Two-way data binding.

  • Double square brackets are one-way, and support only downward data flow.

Server-side model sample code

Here is the server-side sample for tutorial html templates.

PolymerTemplate class

  1. @Tag("my-template")
  2. @HtmlImport("/com/example/PolymerBinding.html")
  3. public class PolymerBindingTemplate extends PolymerTemplate<BindingModel> {
  4. public PolymerBindingTemplate() {
  5. getModel().setHostProperty("Bound property");
  6. }
  7. }

TemplateModel sample

  1. public interface BindingModel extends TemplateModel {
  2. void setHostProperty(String propertyValue);
  3. String getHostProperty();
  4. }

Two-way data binding

For two-way data binding the data flows in both directions client-to-server and server-to-client.

To demonstrate the functionality lets create a TwoWayBindingModel with some a couple of different fields like:

Two-way template model

  1. public interface TwoWayBindingModel extends TemplateModel {
  2. void setName(String name);
  3. String getName();
  4. void setAccepted(Boolean accepted);
  5. Boolean getAccepted();
  6. void setSize(String size);
  7. String getSize();
  8. }

For the server-side PolymerTemplate we will set some default values to the model values and wire listeners for events save and reset as follows:

Two-way binding template

  1. @Tag("two-way-template")
  2. @HtmlImport("/com/example/PolymerTwoWayBinding.html")
  3. public class PolymerTwoWayBindingTemplate
  4. extends PolymerTemplate<TwoWayBindingModel> {
  5. public PolymerTwoWayBindingTemplate() {
  6. reset();
  7. getElement().addPropertyChangeListener("name", event -> System.out
  8. .println("Name is set to: " + getModel().getName()));
  9. getElement().addPropertyChangeListener("accepted",
  10. event -> System.out.println("isAccepted is set to: "
  11. + getModel().getAccepted()));
  12. getElement().addPropertyChangeListener("size", event -> System.out
  13. .println("Size is set to: " + getModel().getSize()));
  14. }
  15. @EventHandler
  16. private void reset() {
  17. getModel().setName("John");
  18. getModel().setAccepted(false);
  19. getModel().setSize("medium");
  20. }
  21. }

We use here the Element::addPropertyChangeListener method to get immediate update for the property values. Another way would be to define an @EventHandler method on the server side which is just called once when a button is pressed similar to the reset() method.

On the client we will use different methods to bind binding the model data:

Name string to an input using:

  • Native input element

  • Polymer element paper-input

Boolean accepted to a checkbox using:

  • Native checkbox input

  • Polymer element paper-check-box

Size string to a select element using:

  • Native select

  • Polymer elements paper-radio-group and paper-radio-button

Note

Native elements need to specify a custom change event name in the annotation using the syntax: target-prop=”{{hostProp::target-change-event}}” see. Two-way binding to a non-Polymer element

Polymer html template

  1. <!-- Import Polymer and Polymer components -->
  2. <link rel="import" href="/bower_components/polymer/polymer-element.html">
  3. <link href="/bower_components/paper-input/paper-input.html" rel="import">
  4. <link href="/bower_components/paper-radio-button/paper-radio-button.html" rel="import">
  5. <link href="/bower_components/paper-radio-group/paper-radio-group.html" rel="import">
  6. <link href="/bower_components/paper-checkbox/paper-checkbox.html" rel="import">
  7. <dom-module id="two-way-template">
  8. <template>
  9. <table>
  10. <tr>
  11. <td>Paper name:</td>
  12. <td>
  13. <paper-input value="{{name}}"></paper-input>
  14. </td>
  15. </tr>
  16. <tr>
  17. <td>Input name:</td>
  18. <td>
  19. <input value="{{name::input}}">
  20. </td>
  21. </tr>
  22. <tr>
  23. <td>Change name:</td>
  24. <td>
  25. <input value="{{name::change}}">
  26. </td>
  27. </tr>
  28. <tr>
  29. <td>Input accepted:</td>
  30. <td>
  31. <input type="checkbox" checked="{{accepted::change}}">
  32. </td>
  33. </tr>
  34. <tr>
  35. <td>Polymer accepted:</td>
  36. <td>
  37. <paper-checkbox checked="{{accepted}}"></paper-checkbox>
  38. </td>
  39. </tr>
  40. <tr>
  41. <td>Size:</td>
  42. <td>
  43. <paper-radio-group selected="{{size}}">
  44. <paper-radio-button name="small">Small</paper-radio-button>
  45. <paper-radio-button name="medium">Medium</paper-radio-button>
  46. <paper-radio-button name="large">Large</paper-radio-button>
  47. </paper-radio-group>
  48. </td>
  49. </tr>
  50. <tr>
  51. <td>Size:</td>
  52. <td>
  53. <select value="{{size::change}}">
  54. <option value="small">Small</option>
  55. <option value="medium">Medium</option>
  56. <option value="large">Large</option>
  57. </select>
  58. </td>
  59. </tr>
  60. </table>
  61. <div>
  62. <button on-click="reset">Reset values</button>
  63. </div>
  64. <slot></slot>
  65. </template>
  66. <script>
  67. class TwoWayBinding extends Polymer.Element {
  68. static get is() {
  69. return 'two-way-template'
  70. }
  71. }
  72. customElements.define(TwoWayBinding.is, TwoWayBinding);
  73. </script>
  74. </dom-module>

Here’s the template representation in the browser:

Template representation

In the template we use two-way bindings for each element and some elements bind to the same property. This will show up in a way that for example the value for name is changed in the paper-input element the value will be reflected to both “Input name:” and “Change name”.

Note

The two input bindings “Input name” and “Change name” have a small difference in the way they work.

Input name binds using {{name::input}} and Change with {{name::change}} the given target-change-event lets Polymer know which event to listen to for change notification.

The functional difference is that ::input will update while typed and ::change when the value for the field changes (so e.g. onBlur event or for enter)

Note

For information on the element <slot></slot> see Using <slot> in PolymerTemplates