Binding Components from 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 @Id annotation allows you to interact with Polymer templates on the server side. You can use the @Id annotation to get a Component or Element reference for an element defined in a JavaScript Polymer template.

In this section, we demonstrate how to use the @Id annotation to reference a JavaScript Polymer template.

Note
The Component or Element must have the same @Tag as the actual element that is referenced. This means that you cannot bind a <span id=”content”></span> to a @Id(“content”) Div content.

Example: MainPage JavaScript Polymer template file.

js

  1. class MainPage extends PolymerElement {
  2. static get template() {
  3. return html`
  4. <div id="header">Main page</div>
  5. <div id="content"></div>
  6. <hr>
  7. <div id="footer">
  8. <a href="mailto:someone@example.com?Subject=Hello" target="_top">Send Mail</a>
  9. </div>`;
  10. }
  11. static get is() {
  12. return 'main-page';
  13. }
  14. }
  15. customElements.define(MainPage.is, MainPage);
  • The html returns a a placeholder div element with a "content" identifier.

  • The div element is mapped to a Div component in the Java code (see below), allowing you to add a Component as a child to it.

Example: Implementing a method in the MainPage class to add a Component to the content of a JavaScript Polymer template element.

Java

  1. @Tag("main-page")
  2. @JsModule("./com/example/main-page.js")
  3. public class MainPage extends PolymerTemplate<TemplateModel> {
  4. @Id("content")
  5. private Div content;
  6. public void setContent(Component content) {
  7. this.content.removeAll();
  8. this.content.add(content);
  9. }
  10. }
  • The @Id annotation maps a component to an element in the JavaScript template on the client with the HTML identifier "content".

  • Vaadin creates a component instance of the declared type automatically and wires it to the template DOM element and the content field in the Java class.

Note
The declared type used in an @Id injection declaration must have a default constructor to be able to instantiate it.
Tip
The @Id annotation can also be used to inject an Element instance instead of a Component instance, if you want to use the lower-level Element API or there is no suitable HTML component available.

Example: Calling the setContent method to set any Component as content for the MainPage class.

Java

  1. MainPage page = new MainPage();
  2. page.setContent(new Label("Hello!"));

Limitations of Mapped PolymerTemplate Components

Out-of-sync Methods

In the Polymer template class example above, you could additionally map the div element with a "footer" identifier using the Div component and @Id("footer") annotation. However, neither the hierarchical structure, nor any attributes or properties, will be available on the server side using the Java API.

The injected Div instance does not have a server-side child, even though the a (anchor) element is available on the client side. The getChildren() method in the injected instance returns an empty Stream. Similarly, the getText() method of the Div instance injected using the @Id("header") annotation, returns an empty string.

To summarize:

  • Server-side Component or Element read methods are not always in sync with the client side.

  • You can still use mutation API methods, like appendChild, setProperty or setAttribute from the server side, without issue.

  • Getter methods return values that are set from the server side only.

Removing Mapped Elements

A virtually-mapped Element is connected to the ShadowRoot of the PolymerTemplate, even if it actually resides deeper in the shadow tree. You cannot remove virtually mapped components from the DOM by removing them on the server side.

Note
You can detect whether a component is used in a PolymerTemplate using the isTemplateMapped method. See the Detecting PolymerTemplate Mappings in Components for more.