简体   繁体   English

还有其他方法可以观察Google Polymer 1.0中对象的子属性的变化吗?

[英]Is there another way to observe for changes to a sub property of an object in Google Polymer 1.0?

I've tried the deep path observation examples on the Google Polymer site but it does not work for my case. 我已经在Google Polymer网站上尝试了深层路径观察示例,但不适用于我的情况。 The sub-property I want to observe is an array that gets changed when I go to a different hash url. 我要观察的子属性是一个数组,当我转到其他哈希URL时,该数组会更改。 The problem is I'm using a library that handles that change so it doesn't use polymer's "push, pop, shift and splice" and without that, Polymer won't recognize the array mutation. 问题是我正在使用一个处理更改的库,因此它不使用聚合物的“推,弹出,移位和拼接”,没有它,聚合物将无法识别数组突变。 Any ideas would be greatly appreciated! 任何想法将不胜感激!

I had what I think is a similar problem when trying to be notified of changes to an array in a computed property. 当试图通知计算属性中数组的更改时,我遇到了类似的问题。

In my case I wanted to know when it's children were modified so that I could recompute something else with the changed children. 就我而言,我想知道何时修改了孩子,以便我可以与更改后的孩子重新计算其他内容。 Here's a snippet of working code from a polymer element: 这是聚合物元素的工作代码片段:

properties: {
    choices: {
        type: Array,
        notify: true,
        value: []
    },
    choicesTransposed: {
        type: Array,
        notify: true,
        computed: '_transposeChoices(choices.*)'
    }
},

_transposeChoices: function(change) {
    var choices = change.base;
    // do something with choices
}

Initially I had _transposeChoices(choices) but this was never called when choices children were modified. 最初我有_transposeChoices(choices)但是在修改choices子项时从未调用过它。 Changing that to _transposeChoices(choices.*) and then using change.base in the computing function is what got things working for me. 将其更改为_transposeChoices(choices.*) ,然后在计算功能中使用change.base才是使我工作的原因。

您可以始终使用observe-js观察对象,并通过自定义事件与Polymer进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM