Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data-binding in <template> on objects attributes have strange behaviour [bug?] #1129

Closed
silentHoo opened this issue Jan 27, 2015 · 1 comment

Comments

@silentHoo
Copy link

Suppose we have the following components <template> code:

<polymer-element name="my-microphone-list">
<paper-radio-group selected="{{ selected.microphoneNumber }}">
  <template repeat="{{ device, index in devices.microphones }}">
    <paper-radio-button
      name="{{ device.id }}"
      label="Microphone {{ index + 1 }}">
    </paper-radio-button>
  </template>
</paper-radio-group>
<polymer-element>

The selected object has an integer value microphoneNumber which indicates the current selected microphone in the given list of microphones. selected.microphoneNumber will change in the components JavaScript code.

When I append my-microphone-list multiple times, everytime I select a radio button in one group, all the other groups will also select the same paper-radio-button. That's strange because selected is NOT a shared variable. When I replace selected.microphoneNumber with a simple variable it works like a charm.

Could someone please explain why this happens?

PS: Here's the code: http://jsfiddle.net/6sqo159z/18/

@silentHoo silentHoo changed the title Data-binding in <template> on objects attributes have strange behaviour Data-binding in <template> on objects attributes have strange behaviour [bug?] Feb 2, 2015
@silentHoo
Copy link
Author

Ok, it seems that I'm in trouble with the "shared state" as mentioned in the Polymer API Guide which tells Important: For properties that are objects or arrays, you should always initialize the properties in the created callback. If you set the default value directly on the prototype (or on the publish object), you may run into unexpected “shared state” across different instances of the same element.

// Good!
Polymer('x-foo', {
  created: function() {
    this.list = []; // Initialize and hint type to be array.
    this.person = {}; // Initialize and hint type to an object.
  }
});

// Bad.
Polymer('x-foo', {
  list: [], // Don't initialize array or objects on the prototype.
  person: {}
});

See below these anchor link: https://www.polymer-project.org/docs/polymer/polymer.html#configuring-an-element-via-attributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant