聚合物-如何将观察者附加到阵列上? [英] Polymer - How do I attach an observer to an array?

查看:66
本文介绍了聚合物-如何将观察者附加到阵列上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将观察者附加到作为数组的聚合物属性上?明确地说,当数组中的项目更改时,我需要回调.为了简单起见,假设我的数组是:

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' }
]

我想要类似的东西:

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

以下方法有效,但显然是不可持续的:

The following works, but is obviously un-sustainable:

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

请注意,在我的情况下,更改来自我可以控制的另一个聚合物元素.因此,如果我能以某种方式触发对控制{ text: 'foo' }的元素的更改,那也将起作用.

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.

推荐答案

要清楚,Polymer会自动观察数组,但是'ArrayObserver'只会告诉您(a)数组本身是否已替换或(b)项添加,删除或重新排列.就像观察对象一样,Polymer不会自动观察子对象的属性.

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.

这篇关于聚合物-如何将观察者附加到阵列上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆