It hard to describe this issue, as I do not really understand it. so I will just provide an example with the expected result.
The element will just observer new nodes and add it to an array
class TestinbgElement extends Polymer.Element {
static get is() { return 'testinbg-element'; }
static get properties() {
return {
items: {
type: Array,
value: []
}
};
}
connectedCallback() {
super.connectedCallback();
this.__observer = new Polymer.FlattenedNodesObserver(this.$.slot, (info) => {
this._processNewNodes(info.addedNodes);
});
}
disconnectedCallback() {
super.disconnectedCallback();
this.__observer.disconnect();
}
_processNewNodes(nodes) {
nodes.forEach((node) => {
if(node.nodeType === Node.ELEMENT_NODE) {
this.push('items',node);
}
});
console.log(this.items);
}
}
<testinbg-element>
<div>I am content 1</div>
<div>I am content 2</div>
</testinbg-element>
<testinbg-element>
<div>I am content 3</div>
<div>I am content 4</div>
</testinbg-element>
Array >> (2) [div, div] // for the first element instance
Array >> (2) [div, div] // for the second element instance
Array >> (2) [div, div] // for the first element instance
Array >> (4) [div, div , div , div] // for the second element instance
https://github.com/hyyan/strange-behavior-FlattenedNodesObserver~~
http://jsbin.com/jurozobuja/edit?html,console,output
JSBin that shows the issue: http://jsbin.com/jurozobuja/edit?html,console,output
Thanks @TimvdLippe I was just preparing one :)
You need to change line 35 to value: () => {return []} so each instance gets its own array. Its easy to overlook in the docs.
@morbidick That's great , thanks , this will solve the problem
Most helpful comment
You need to change line 35 to
value: () => {return []}so each instance gets its own array. Its easy to overlook in the docs.