简体   繁体   English

更改后,聚合物阵列不会更新UI

[英]Polymer array won't update UI when changed

A lot of Polymer 1.0 problems today, 今天有很多Polymer 1.0问题,

My HTML look like this: 我的HTML看起来像这样:

<section class="layout vertical" id="onlineList">
        <paper-item class="subdue layout horizontal center">Online Now</paper-item>
        <template is="dom-repeat" items="{{cats}}" as="cat">
            <paper-item>{{cat}}</paper-item>
        </template>
</section>

As you can see I'm trying to loop out each cat in the array cats . 正如你可以看到我试图环出每个cat在阵列cats My problem is that the cats -array changes about 4 times. 我的问题是cats -array大约更改了4次。 The first time it has one cat , second time two..and so on, until there is no more changes. 第一次有一只cat ,第二次有两只猫,依此类推,直到没有更多变化为止。 But it seems that the only change that is propogated to the UI is the first cat . 但是似乎向UI传播的唯一变化是第一cat

The cats array is changed in a callback called presenceChanged which PubNub calls when the "online"-list changes, see below: 猫阵列在一个叫做回调改变presenceChanged这PubNub调用时,“在线” -list变化,见下图:

template.presenceChanged = function(e) {
    var l = template.$.sub.presence.length;
    var d = template.$.sub.presence[l - 1];

    // who are online
    if(d.action === 'join') {
        if(d.uuid.length > 35) { // console
            d.uuid = 'the-mighty-big-cat';
        }
        onlineUuids.push(d.uuid);
    } else {
        var idx = onlineUuids.indexOf(d.uuid);
        if(idx > -1) {
            onlineUuids.splice(idx, 1);
        }
    }

    // display at the left column
    template.cats = onlineUuids;

    //.. some code
};

So, just to clarify. 因此,仅需澄清一下。 The presentChanged-callback is called, lets say 4 times. presentChanged-callback被调用,可以说4次。 And each time the list will contain one more cat (so it's called once for every user which is in the "room"). 每次列表中将再包含一只猫(因此,“房间”中的每个用户都将其称为一次)。 In the "last" call it will look like: ["cat_1","cat_2","cat_3","cat_4"] but only ["cat_1"] will be shown UI-wise, why? 在“上一个”调用中,它看起来像: ["cat_1","cat_2","cat_3","cat_4"]但只有["cat_1"]会在用户界面上显示,为什么? :/ :/

You need to use the the array mutation methods provided by the Polymer elements (see docs on dom-repeat ): 您需要使用Polymer元素提供的数组突变方法(请参阅dom-repeat上的文档 ):

Mutations to the items array itself (push, pop, splice, shift, unshift) must be performed using methods provided on Polymer elements, such that the changes are observable to any elements bound to the same array in the tree. 必须使用Polymer元素上提供的方法对items数组本身进行更改(推,弹出,拼接,移位,不移位),以便对绑定到树中相同数组的任何元素都可以观察到更改。 For example 例如

In your example you need to use this.push('cats', d.uuid); 在您的示例中,您需要使用this.push('cats', d.uuid); instead of the push method on the actual array. 而不是实际数组上的push方法。 (same thing applies to the splice , etc) (同样的情况适用于splice等)

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

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