Detecting whether your component is part of a PolymerTemplate

When creating your own component, you may want to execute custom logic depending on the component was mapped in a template (by being injected via @Id or @Uses) or was created directly using one of its constructors.

To do that, you can call the isTemplateMapped method from your component. For example:

@Tag("div")
public class MyComponent extends Div {

  public MyComponent() {
    if (!isTemplateMapped()) {
      getElement().getStyle().set("color", "red");
    }
  }
}

In this example, the component only sets a style by default when the component is not part of a template.

To know more about @Id mappings, check the Binding Components from a PolymerTemplate tutorial.