Binding Model Data in Polymer Templates

Note
Use Lit templates instead
Lit templates are recommended. Polymer templates are available in the next long term supported Vaadin version (LTS), but they are deprecated.

The way model values are bound to different parts of the element tree that is defined by the template is at the core of the PolymerTemplate API. See Creating A Simple Component Using the PolymerTemplate API for a simple example of how to set model values.

The first three sections below demonstrate how to bind text content, property values, and attribute values. The JavaScript template examples in these sections all use the following example Java template and model class

Example: PolymerBindingTemplate template class.

Java

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

Example: BindingModel model interface.

Java

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

Binding Text Content

To bind text content in a JavaScript Polymer template, use the \[[*propertyName*]] syntax inside a tag. Double square brackets ([[ ]]) indicate a one-way binding.

Example: Binding text content in a JavaScript Polymer template.

js

  1. class MyTemplate extends PolymerElement {
  2. static get template() {
  3. return html`<div>[[hostProperty]]</div>`;
  4. }
  5. static get is() {return 'my-template'}
  6. }
  7. customElements.define(MyTemplate.is, MyTemplate);

Binding Property Values

To set an element property value based on a model, use the property name in the attribute format, by using the dash-case format, sometimes referred to as kebab-case, e.g. my-property-name (not camelCase myPropertyName).

Example: Setting an element property based on a model in a JavaScript Polymer template.

js

  1. return html`<my-element my-property="[[hostProperty]]"></my-element>`;
  • This 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.

  • name="binding" (without brackets) defines that the element attribute named name should have the value binding, regardless of any value in the model.

Polymer cannot bind certain common native element properties directly, because the binding causes issues on one or more browsers. In these cases, you can use attribute bindings instead. See Native properties that don’t support property binding for more.

Binding Attribute Values

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

To explicitly bind to an attribute, use the attribute name followed by a dollar sign ($).

Example: Binding to an attribute in an JavaScript Polymer template.

js

  1. return html`<div something$="[[hostProperty]]"></div>`;

or

js

  1. return html`<a href$="[[hostProperty]]"></a>`;

Two-way Versus One-way Bindings

Text surrounded by double curly bracket {{ }} or double square bracket [[ ]] delimiters identifies the host data that is bound:

  • Double curly brackets ({{ }}) indicate two-way bindings: both server-to-client and client-to-server data flow.

  • Double square brackets ([[ ]]) indicate one-way bindings: only server-to-client data flow.

Creating Two-way Data Bindings

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

To demonstrate we create:

  • A two-way binding model class with a number of fields.

  • A Java template class that sets default values for the model, and adds listeners for save and reset events.

  • An associated JavaScript Polymer template.

Example: TwoWayBindingModel with name, accepted and size fields.

Java

  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. }

Example: PolymerTwoWayBindingTemplate Java template class that defines save and reset event property change listeners, and sets default values for the name, accepted and size fields.

Java

  1. @Tag("two-way-template")
  2. @JsModule("./com/example/two-way-template.js")
  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. }
  • The Element::addPropertyChangeListener method gets immediate updates when the property values change. As an alternative, you could define an @EventHandler method on the server side and add appropriate event handers in the template.

  • On the client, we use the following methods to bind the model data (see JavaScript template below):

    • name string to an input using:

      • Native input element.

      • Polymer element paper-input.

    • accepted boolean 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 target-prop=”{{hostProp::target-change-event}}” syntax. See Two-way binding to a non-Polymer element in the Polymer 3 documentation for more.

Example: Polymer JavaScript template.

js

  1. import {PolymerElement,html} from '@polymer/polymer/polymer-element.js';
  2. import '@polymer/paper-input/paper-input.js';
  3. import '@polymer/paper-radio-button/paper-radio-button.js';
  4. import '@polymer/paper-radio-group/paper-radio-group.js';
  5. import '@polymer/paper-checkbox/paper-checkbox.js';
  6. class TwoWayBinding extends PolymerElement {
  7. static get template() {
  8. return html`
  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. }
  66. static get is() {
  67. return 'two-way-template';
  68. }
  69. }
  70. customElements.define(TwoWayBinding.is, TwoWayBinding);
  • We use two-way bindings for each element.

  • Some elements bind to the same property. For example, when the value for name is changed in the paper-input element, the new value reflects in both Input name and Change name.

  • The two input bindings, Input name and Change name, work in slightly different ways:

    • Input name binds using {{name::input}} and Change name binds using {{name::change}}. The given target-change-event lets Polymer know which event to listen to for change notifications.

    • The functional difference is that ::input updates during typing, and ::change updates when the value of the field changes, for example an onBlur event or Enter key press.

Here’s the template representation in the browser:

Template representation

For information on the <slot></slot> element, see Using <slot> in PolymerTemplates for more.