Binding Components from a PolymerTemplate
By using the @Id
annotation, you can get a server-side Component
or Element
, for an element only defined in the PolymerTemplate html file for the client side to use on the server.
Note | The Component /Element needs to have the same @Tag as the actual element that is wired. This means that you can not wire a <span id=”content”></span> to a @Id(“content”) Div content |
Here is the content of the HTML template file which has a placeholder div
element with a "content"
identifier. This element is mapped to a Div
component in Java code below to set a as a child for it.
HTML
Java
The @Id
annotation is used to map a component to an element created by the template on the client that has the identifier "content"
.
A component instance of the declared type is created automatically and wired to the actual dom element and injected into the ‘container’ field.
Java
In the Polymer template class example above, the div
element with "footer"
identifier could also be mapped via Div
component and @Id("footer")
annotation. But neither its hierarchical structure nor attributes/properties are available on the server side via API. The injected Div
instance doesn’t have any server side child even though there is the anchor a
element available on the client side. The injected instance getChildren()
method returns an empty Stream
.
Similar to this the getText()
method of the instance injected via @Id("header")
returns an empty string.
Note | The declared type used in an @Id injection declaration must have a default constructor to be able to instantiate it. |
Note | You can detect whether a component is part of a Template by using the isTemplateMapped method. See the Integrating components in a PolymerTemplate tutorial for more details. |