Backbone 集合未能删除所有给定模型

Backbone collection failing to remove all given models

获取集合后,我在最后添加 "fake" 模型,以使我的 flex 网格正常工作。对这个解决方案不满意,但找不到更好的解决方案。

我想支持将更多模型加载到集合中。因此,在同步时,我删除了旧的假模型(否则最终会出现在集合的中间),然后在最后重新添加它们。

onSync: ->
  modelsToRemove = @where({id: 0})
  console.log modelsToRemove, modelsToRemove.length
  // this prints:
  // [child, child, child, child, child]  5
  // which is expected
  @remove(modelsToRemove)

  modelsToRemove2 = @where({id: 0})
  console.log modelsToRemove2, modelsToRemove2.length
  // this should print
  // [] 0
  // right?
  // but it prints:
  // [child]  1
  // and the cid of the child is actually present in the first list...

  @add({}) for [1..@fakeCount]

为什么 remove 会删除除 1 以外的所有假模型?

编辑:使用 cids 使其正常工作。

  modelsToRemove = _.map @where({id: 0}), (m, i) -> { cid: m.cid }
  @remove(modelsToRemove)
  @add({}) for [1..@fakeCount]

这个问题好像是因为几个型号除了cid完全一样?

所有这些假模型都具有相同的 ID 这一事实打破了 #get

在 Github 问题上给我的解决方案是给他们不同的 ID,以及 fake 属性.