Use ArrayObserver declaratively outside <template repeat="...">

57 views
Skip to first unread message

Martin Kleinschrodt

unread,
Jan 15, 2015, 4:19:46 AM1/15/15
to polym...@googlegroups.com
Lets say I have a computed property like this:

computed:{
  rows
: "buildRows(data, numCols)"
}

Where 'data' is an array and I want 'rows' to be updated any time the anything in the 'data' array changes. Currently, polymer expressions uses a regular observer (path observer I think) to listen for changes to the 'data' object. Unfortunately, this means that the 'rows' property is only being updated when the 'data' array is replaced with a new object, not if elements are added or removed. Is there any way to tell polymer to use an ArrayObserver in this case?

Aleks Totic

unread,
Jan 18, 2015, 5:29:37 PM1/18/15
to polym...@googlegroups.com
 > Is there any way to tell polymer to use an ArrayObserver in this case?

I have not been able to do this cleanly. Cleanest I've gotten is using an extra variable together with ArrayObserver to trigger data recomputation.

Polymer team suggestion: it'd be very handy to have a way to manually trigger recomputation of computed properties. It'd avoid the "extra var to trigger changes" hack.

created: {
   
this.dataChanged = 0;
   
var obs = new ArrayObserver(this.data);
   obs
.open( function() { this.dataChanged++ }.bind(this));
}
computed
: {
   rows
: "buildRows(data, numCols, dataChanged)"
}

Reply all
Reply to author
Forward
Message has been deleted
0 new messages