考虑以下代码:更新阵列不会反映到DOM
<template is="dom-repeat" items="{{chats}}" as="item">
<p>{{item.name}}</p>
<span>{{item.unreadcount}}</span>
</template>
我似乎无法确保更换阵列(完全,不只是更新一个的属性的元素)更新dom。
Updating(升级)我想是这样的:
var c = this.chats;
c.forEach(function(e) {
e.unreadcount = Math.floor(Math.random() * 10) + 1;
}.bind(this));
// none of the following approaches has any effect:
this.chats = c;
this.set("chats", c);
this.notifyPath("chats", c);
我如何能实现,如果数组被彻底改变重新渲染DOM?
编辑
感谢玛丽亚暗示我想通了,如何做到这一点。
仅使用splice()
是不够的,您还需要使用Polymer的函数来修改阵列本身:this.set(..., ...)
。
for (i = 0; i < this.chats.length; i++) {
this.set('chats.'+i+'.unreadcount', Math.floor(Math.random() * 10) + 1);
}
this.chats = this.chats.slice();
我的额头开始,因为与聚合物开始,由于我的头撞在墙上的大规模撞压平
如果你用'this.set'完成所有的操作,你就不需要'this.chats = this.chats.slice();'。试图删除它,并检查它是否仍然有效? – Maria