Polymer 1.0 watchers - not working with an array

I set an observer to catch all the events recognized by the policy on the property, which is an array, but I will catch it to catch the change. In my example below, my observer function "bigup" is called only when the "bigs" property is first initialized.

<dom-module id="parent-page"> <template> <paper-button on-click="updateAll">Update</paper-button> </template> <script> var temp=[]; temp.push({'conversation':[{'message':'hello'}]}); Polymer({ is: 'parent-page', properties: { bigs: { type: Array, value: temp } }, observers: [ 'bigup(bigs.*)' ], updateAll: function(){ this.bigs.push({'conversation':[{'message':'hola'}]}); console.dir(this.bigs); }, bigup: function(){ console.log('big Up'); } }); </script> 

I also tried using bigs.push as an observer, but was not successful. One part that I don’t understand is that if I add the following line to my “updateAll” function, the observer will catch this change and run “bigup”.

 this.bigs=[]; 
+5
source share
2 answers

You need to use the push method from Polymer instead of this.bigs.push .

So, replace this line with the code for

 this.push('bigs', {'conversation':[{'message':'hola'}]}); 

For more information, see the link.

+2
source

This article was useful to me, it covers both the method of registering an observer and the methods of splicing, as suggested by Justin XL.

Observer Registration :

 properties: { users: { type: Array, value: function() { return []; } } }, observers: [ 'usersAddedOrRemoved(users.splices)' ], 

Calling splicing methods using Polymer 1.0:

 this.push('users', 'TestUser'); 

https://www.polymer-project.org/1.0/docs/devguide/properties.html#array-observation

FYI - this will NOT work in all cases (my initial idea) When you register an observer in a property declaration as follows:

  properties: { users: { type: Array, value: function() { return []; }, observer: 'usersAddedOrRemoved' } }, 

In this case, the usersAddedOrRemoved method usersAddedOrRemoved called only when a new array is assigned to the user object. However, it will not fire when you mutate an array by clicking, popping, splicing, etc.

+5
source

All Articles