本文介绍了Polymer 1.0观察者-不适用于数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个观察器,以捕获数组属性上所有聚合物识别的事件,但是我捕获到它以捕获更改.在下面的示例中,仅当属性"bigs"首次初始化时,观察者函数"bigup"才被调用.

I set an observer on to catch all polymer recognized events on an property that is an array, but I catch get it to catch the change. In my example below my observer function "bigup" only gets called on when the property, "bigs" is first initialzed.

<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>

我也尝试在观察者中使用bigs.push,但没有成功.我不了解的一部分是,如果将以下行添加到我的"updateAll"函数中,观察者将捕获更改并触发"bigup".

I also tried to use bigs.push in the observer but had no success. One part I don't understand is that if I add the following line to my "updateAll" function, the observer catches the change and fires "bigup".

this.bigs=[];

推荐答案

您需要使用Polymer的push方法来代替this.bigs.push.

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

因此,用

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

有关更多信息,请查看此链接.

For more info have a look at this link.

这篇关于Polymer 1.0观察者-不适用于数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:10