0

I want to have a polymer element which shares an array to all instances. THis is possible if I do not use a function to instantiate the array. This works fine. BUt when I change the array Elements with this.push("ArrayPath", Value), than only the current instance is notified, but not all. What I want todo is to have a computed boolean property, which listens to the array change, what I have:

Polymer({
        is: 'data-holder',
        properties: {
            editedFields: {
                type: Array,
                notify: true,
                value: []
            },
            unsaved: {
                type: Boolean,
                notify: true,
                reflectToAttribute: true,
                computed: '_isUnsaved(editedFields.*)'
            }
        },


       _isUnsaved: function(array) {
           return (array.base.length > 0) ? true : false;
       },
       _addItem: function(value) {
           this.push("editedFields", value);
       }, 
....

ok so far so good. With _addItem function I add an item to the editedFields array and my _isUnsaved function gets called. This is fine. But this is only called within this instance where I call the _addItem method. the array "editedFields" is changed in all instances, but the "unsaved" property is only notified within the current instance?

How can I manage it to get all instances listen to the array change? So that all instances get the attribute "unsaved" to the element declaration?

btw. the value parameter of the _addItem method is an object to ensure uniqueness

GobiRan
  • 154
  • 1
  • 8
  • 2
    Did you read http://stackoverflow.com/questions/42011092/best-way-to-communicate-between-instances-of-the-same-web-component-with-polymer – Supersharp Feb 21 '17 at 14:28
  • thank you @Supersharp I have seen it and it seems to be a good solution. But still I wonder why this is not working without your Behavior implementation. you initialize the array value with a function wich makes the array instance unique and not shared over all instances. But when I do not use a function the array is already shared, so why is it not notified. Otherwise it makes no sense to share the array data by default from polymer. – GobiRan Feb 21 '17 at 15:21
  • ok probably it is cause I use this.push, where this is the current instance. So if there is no possiblity to make sth. like Polymer.Base.push I think your solution is the best one ... but maybe some knows more then me :) – GobiRan Feb 21 '17 at 15:30
  • 1
    It's not my solution :-) Mine would be to use a simple subscriber/publisher or observer/observable design pattern... – Supersharp Feb 21 '17 at 15:39
  • Ah sorry, overlooked it :) yes, "alesc" solution was in theory also a simple subscriber/publisher pattern ... just a little more handy as behavior. Thanks for the hints @Supersharp – GobiRan Feb 21 '17 at 15:43
  • anyway, this solution worked for me, just tested. Thanks again – GobiRan Feb 21 '17 at 16:48

0 Answers0