Using Beans with a PolymerTemplate Model

For an introduction to templates see Creating A Simple Component Using the Template API and template models see Binding Model Data in a PolymerTemplate.

Using beans in models provides you with an easy way of defining your model or reusing existing beans for the model. You can use any bean together with the model as long as it has a public no-arg constructor.

A typical use case would be a form where you want to edit the contents of one or more entities. The template for a form for editing a Person bean might for instance look like:

MyForm html template

  1. <dom-module id="my-form">
  2. <template>
  3. <div>
  4. <div>
  5. <span>First name</span>
  6. <input value="[[person.firstName]]" />
  7. </div>
  8. <div>
  9. <span>Last name</span>
  10. <input value="[[person.lastName]]" />
  11. </div>
  12. <div>
  13. <span>Age</span>
  14. <input value="[[person.age]]" />
  15. </div>
  16. </div>
  17. <div>
  18. <button on-click="setNameToJeff">Set Name To Jeff</button>
  19. </div>
  20. </template>
  21. <script>
  22. class MyForm extends Polymer.Element {
  23. static get is() {
  24. return 'my-form'
  25. }
  26. }
  27. customElements.define(MyForm.is, MyForm);
  28. </script>
  29. </dom-module>

If you have a Person bean that looks like:

Person bean

  1. public class Person {
  2. private String firstName, lastName;
  3. private int age;
  4. private Long id;
  5. public Person() {
  6. // Needed for TemplateModel
  7. }
  8. public Person(String firstName, String lastName, int age) {
  9. this.firstName = firstName;
  10. this.lastName = lastName;
  11. this.age = age;
  12. }
  13. public String getFirstName() {
  14. return firstName;
  15. }
  16. public void setFirstName(String firstName) {
  17. this.firstName = firstName;
  18. }
  19. public String getLastName() {
  20. return lastName;
  21. }
  22. public int getAge() {
  23. return age;
  24. }
  25. public Long getId() {
  26. return id;
  27. }
  28. }

You can specify your model to use a Person bean by adding a setter and a getter for it:

FormModel model definition as an interface

  1. public interface FormModel extends TemplateModel {
  2. @Exclude("id")
  3. void setPerson(Person person);
  4. Person getPerson();
  5. }
Tip
You can use the @Include annotation and provide a list of properties that you want import (all other properties will be excluded) or @Exclude for a list you want to exclude. In this specific case the @Include({“firstName”,”lastName”,”age”}) would be the same as the shown @Exclude(“id”) annotation.
Tip
You can include and exclude sub bean properties by using the dot-annotation, e.g. @Exclude(“account.password”).
Note
You don’t have to declare the Person type as a class. It may be declared as an interface. And there is no need to create its instance and use setter. You just may call getPerson() and the model will return for you an empty proxy object which you can use to set its property directly.
Note
When setting backend data directly to the model, you must exclude any bean properties that would lead to circular dependencies. For instance, if a Patient bean would have a property Doctor doctor, the Doctor bean must not have a property of type Patient (or as a generic type for a collection property), as that would lead to a circular dependency.

In the template you can initialize the model by setting a new person instance to it:

Form template class

  1. public class Form extends PolymerTemplate<FormModel> {
  2. public Form() {
  3. Person person = new Person("John", "Doe", 82);
  4. getModel().setPerson(person);
  5. }
  6. }
Note
If you later on update the Person person bean created in the constructor, nothing will happen to the model. The bean values are copied by setPerson and the bean is not attached to the model in any way.

To update the values in the model, you can use getModel().getPerson() or getModel().getProxy("person", Person.class) to get a proxy Person object, which is attached to the model. Any changes you do to that proxy object will automatically update the model:

Form template with an eventHandler for nameChange

  1. public class Form extends PolymerTemplate<FormModel> {
  2. @EventHandler
  3. public void setNameToJeff() {
  4. getModel().getPerson().setFirstName("Jeff");
  5. }
  6. }
Note
Your bean will never be stored as a bean in the model, instead the individual parts of the bean will be stored. No method will ever return the original bean to you.
Note
The proxy bean returned by the getter is not meant to be passed on to an EntityManager or similar. It is purely meant for updating the values in the model.
Warning
There is at the time of writing no way to get a detached bean from the model.

When wanting to use model data with an entity manager you need to re-instantiate a new entity and set the values using the getters for the item gotten from the model.

Note! in the example that we can’t send the Person object from the model directly to the service as the the object is proxied and only returns data when the getters are used.

Java

  1. public class OrderForm extends PolymerTemplate<FormModel> {
  2. public interface FormModel extends TemplateModel {
  3. @Exclude("id")
  4. void setPerson(Person person);
  5. Person getPerson();
  6. }
  7. public OrderForm() {
  8. Person person = new Person("John", "Doe", 82);
  9. getModel().setPerson(person);
  10. }
  11. @EventHandler
  12. public void submit() {
  13. Person person = getModel().getPerson();
  14. getService().placeOrder(new Person(person.getFirstName(), person.getLastName(), person.getAge()));
  15. }
  16. private OrderService getService() {
  17. // Implementation omitted
  18. return new OrderService();
  19. }
  20. }