如何使用新阵列更新dom-repeat列表

时间:2016-02-13 22:22:24

标签: javascript polymer redux

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

但是,我使用的是Polymer,并且有一个todo-list元素。在我的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操作的调度,并在传递给上面的subscribe函数的函数中收到更新,并更新状态。

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

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

2 个答案:

答案 0 :(得分:1)

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());
}

使用列表中的切片()。您插入this.items state.todos

问题是如果state.todo是相同的数组聚合物将不知道内容是否已更改。通过这种方式,您可以创建新的数组,因此聚合物现在将更改阵列。

答案 1 :(得分:0)

  

确保始终返回新状态,并且新状态中的所有属性也是新状态。不要改变你在州上保存的列表,总是用新的列表替换它们。

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