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

How do I update dom-repeat list with new array

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

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

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 动作,我在传递给上面的订阅函数的函数中收到了一个更新,并且状态被更新了。

在阅读 dom-repeat 和 Polymer 中的列表的文档时,似乎不支持完整更新和重新呈现列表,而是文档提到使用 pop、push 等方法或拼接更新列表(数组)。但我真的不想那样。

知道如何使用新的完整状态更新列表吗?因为现在,列表已更新但未重新呈现,因此不会显示更改。

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是同一个数组聚合物将不知道内容被改变了。通过这种方式,您可以创建新阵列,因此聚合物现在将改变阵列。

Just make sure that you always return a new state, and that any properties in that new state are also new. Don't mutate lists that you save on your state, always replace them with new ones.

我只想说,我花了整整一周的时间尝试不同的解决方案,而这个解决方案奏效了。谢谢你的回答!