vaadin / flow

Vaadin Flow is a Java framework binding Vaadin web components to Java. This is part of Vaadin 10+.
Apache License 2.0
600 stars 165 forks source link

Observing objects set by a `TemplateModel` lead to unexpected results #3392

Open gilberto-torrezan opened 6 years ago

gilberto-torrezan commented 6 years ago

Given this PolymerTemplate:

@Tag("my-component")
@HtmlImport("my-component.html")
public class MyComponent extends PolymerTemplate<MyComponent.Model> {

    public static class Person {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static interface Model extends TemplateModel {
        Person getPerson();
        void setPerson(Person person);
    }

    public void setPerson(Person person) {
        getModel().setPerson(person);
    }
}

... and this template:

<dom-module id="my-component">
  <template>
    <h1>My Component</h1>
  </template>
  <script>
    class MyComponent extends Polymer.Element {
      static get is() { return 'my-component'; }
      static get properties() {
        return {
          person: {
            type: Object,
            notify: true,
            reflectToAttribute: true,
            value: {},
            observer: '_myListener'
          }
        };
      }
     _myListener(value) {
        console.log(value);
    }
  }
  window.customElements.define(MyComponent.is, MyComponent);
  </script>
</dom-module>

When setting the model object at the server-side, the received object at the client-side is empty at first (not null, but empty), and then after that the properties are populated. But even after the properties are set, the object is not quite the same as the Java counterpart - there's an extra nodeId property in the object, that wasn't part of the object in the server-side.

screenshot from 2018-01-24 17-55-18

A simple project showcasing the problem can be accessed here: https://github.com/gilberto-torrezan/flow-polymer-component

Issue reported by @tomasell via our Gitter channel https://gitter.im/vaadin-flow/Lobby

heruan commented 6 years ago

Maybe a WeakMap could be a valid alternative to the foreign nodeId attribute to keep track of JSON nodes?