1

I've been trying to figure this out for a bit, but perhaps someone a bit smarter than me already knows this one.

I'm using Backbone 1.0.0. I have a collection that is updated by a model's fetch method; in the model's parse stage one of the Ajax response attributes is actually a collection within, and I'm storing that out separately. I'm using the Backbone.Collection.set() method to update the collection; it comes in as an array of object literals.

I'm having some issues with the Backbone.Collection.set() method. Everytime, it seems to remove all the models in the collection and then adding them all back, even when none of them has changed. I can see all the events by binding to collection all.

How is Backbone determining whether or not a model is the same, changed, or removed or updated? I'm looking at the source but not really getting a sense of it. Is it looking for some kind of identifying attribute for each model?

4

1 回答 1

1

该方法通过将模型传入并查看它是否返回任何内容Collection.set来确定集合中是否存在模型。Collection.get

Collection.get使用模型的idand 来寻找现有的匹配,cid如果它没有id. 由于cid总是在内部生成,如果传递给的对象set没有 anid它永远不会找到匹配项,并且将始终被视为新对象。

如果存在现有模型,set则使用新属性调用其方法。根据Model.set,如果新属性不等于旧属性,则会触发更改事件。

于 2013-06-14T18:25:55.730 回答