gpt4 book ai didi

javascript - 如何用新数组更新 dom-repeat 列表

转载 作者:行者123 更新时间:2023-11-30 00:14:07 24 4
gpt4 key购买 nike

所以,我正在尝试学习 Redux。顺便说一句,很棒的图书馆,真的很容易掌握。

但是,我使用的是 Polymer,并且有一个 todo-list 元素。在我的待办事项列表元素中,我有一个属性定义,

properties: {
items: {
type: Array,
notify: true
}
},

和一个准备好的函数,

ready: function() {
var that = this;
store.subscribe(function() {
var state = store.getState();
console.log('State have been updated ', state);
that.items = state.todos;
that.notifyPath('items', that.items);
console.log('items ', that.items);
});

store.dispatch(actions.requestTodos());
}

Redux 部分工作得很好。在另一个元素(不重要)中,我执行了 ADD_TODO 操作的分派(dispatch),我在传递给上面的订阅函数的函数中收到更新,并且状态被更新。

当阅读 dom-repeat 和 Polymer 列表的文档时,似乎不支持列表的完整更新和重新渲染,相反文档提到使用 pop、push 或 splice 等方法更新列表(大批)。但我真的不想那样。

关于如何使用新的完整状态更新列表的任何想法?因为现在,列表已更新但未重新呈现,因此不会显示更改。

最佳答案

ready: function() {
var that = this;
store.subscribe(function() {
var state = store.getState();
console.log('State have been updated ', state);
that.set('items',state.todos.slice());

console.log('items ', that.items);
});

store.dispatch(actions.requestTodos());
}

在列表上使用 slice()。你插入到 this.items state.todos

问题是如果 state.todo 是同一个数组 polymer 将不知道内容被改变了。通过这种方式,您可以创建新阵列,因此 polymer 现在将改变阵列。

关于javascript - 如何用新数组更新 dom-repeat 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35386033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com