Flux - 谁应该更改集合中的模型中的数据

Flux - who should change data in models which are in collection?

本文关键字:集合 模型 数据 Flux      更新时间:2023-09-26

我有模型的骨干集合和此集合的列表视图。

<ul>
  <li><input type="checkbox"/> <span>Title</span></li>
  ...
</ul>

当用户单击复选框时,我执行此代码

Actions.save({id: model.cid, data: {select: true}})

此操作触发事件save-model调度程序中,此处的问题 - 谁应处理此事件?

我有两个选择:

1) 收集

Dispatcher.on('save-model', function (event) {
  var model = collection.get(event.cid);
  if (model) {
    model.set(event.data);
  }
});

2)集合中的每个模型都应该监听Dispather

Dispatcher.on('save-model', function (event) {
  if (model.cid === event.cid) {
    model.set(event.data);
  }
});

我会选择选项 #1 - 集合。我的观点是,这样事件只需要处理一次。如果在模型中绑定,假设您的集合有 1k 个模型,则会触发 1k 事件以检查其是否匹配,然后设置属性。