Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymer - How do I attach an observer to an array?

How do I attach an observer to a polymer attribute that is an array? To be clear, I want callbacks when items in the array change. For simplicity, let's say my array is:

[
    { text: 'foo' },
    { text: 'bar' }
]

I want something like:

observe : {
    'items.text' : 'itemsChanged'
}

The following works, but is obviously un-sustainable:

observe : {
    'items[0].text' : 'itemsChanged',
    'items[1].text' : 'itemsChanged'
}

Note that in my case, the changes are coming from another polymer element that I have control over. So if I could somehow trigger a change from the element that has control over { text: 'foo' }, that would work as well.

like image 703
Nobody Avatar asked Jul 08 '14 18:07

Nobody


1 Answers

To be clear, Polymer will automatically observe Arrays, but an 'ArrayObserver' will only tell you if (a) the Array itself is replaced or (b) items are added, removed, or rearranged. Just like observed objects, Polymer does not automatically observe properties of sub-objects.

the changes are coming from another polymer element that I have control over

This is usually the case and we typically have the element doing the changing fire an event for communication.

like image 166
Scott Miles Avatar answered Oct 05 '22 05:10

Scott Miles