backbone icon indicating copy to clipboard operation
backbone copied to clipboard

Merge option for Collection.set is not working in a specific case

Open tkriplean opened this issue 13 years ago • 2 comments

When adding models to a collection, duplicates are not merged when Collection.model is defined as a method that instantiates an appropriate model (for polymorphic collections). This happens when the models being added are still defined as objects, not instantiated models.

A minimum example: http://jsfiddle.net/tkriplean/xLQHf/3/

The issue occurs in Collection.set on line 683 (for v1.1.0):

 id = attrs[targetModel.prototype.idAttribute];

In the case described, targetModel will be a method, not a model class, and therefore targetModel.prototype.idAttribute is always undefined. Which subsequently means that the already existing model is never found.

An ugly workaround is to instantiate the list of models first and then add them to the target collection.

Anyway, just wanted to report this issue :-)

Thanks for Backbone, it is great!

tkriplean avatar Dec 02 '13 19:12 tkriplean

Have a look at #2836 and #2775

I think this is also fixed on the master branch

j03w avatar Dec 02 '13 21:12 j03w

Thanks @j03w I tried out the master branch and it is indeed fixed there. Sorry for the redundant issue.

tkriplean avatar Dec 02 '13 21:12 tkriplean