user736893
user736893

Reputation:

Callback instance conflict?

I have the following callback in a polymer element that is only supposed to fire when data is received with a specific dataid value.

Polymer({
    is: 'widget-singlevalue',
    properties: {
        dataid: {
            type: String
        },
        value: {
            type: String,
            value: 'Loading'
        },
        sparks: {
            type: Object,
            value: []
        }
    },
    ready: function() {
        console.log(this.dataid);
        var self = this;
        registerCallback(this.dataid, function (data) {
            var y = data.data[0];
            var i = y[Object.keys(y)[0]];
            self.push('sparks', i);
            console.log(self.dataid + ": " + self.sparks);
            self.value = i;
        });
    }
});

Elements:

<widget-singlevalue name="Retrieves Per Minute" dataid="AvgRetPerMin"></widget-singlevalue>

<widget-singlevalue name="Total" dataid="Total"></widget-singlevalue>

The value seems to always be right, however when I look at the sparks arrays on each widget/element, they are mixed up. Here is my current console output:

AvgRetPerMin: 63.81,63.81,14891940,14891940,57.67,57.67,14892034,14892034,57.33,57.33
Total: 63.81,14891940,14891940,57.67,57.67,14892034,14892034,57.33,57.33,14892034

Total.Sparks should only ever have values in the 14 million range, the other should be double digits with a decimal.

Upvotes: 0

Views: 54

Answers (1)

a1626
a1626

Reputation: 2964

Few mistakes that that are present in your code are

  • sparks type is Object but value an Array. So change sparks type to Array
  • And for both arrays and objects use function to return value. As per Polymer recommendation it always use function to return value for Object and Arrays. The reason behind this is so that each instance of the element has its own copy of the Object/Array

    value:function(){
            return []
          }
    

Upvotes: 1

Related Questions