mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-30 03:00:06 -04:00
Fix #2976 Do not add multiple models with same id
This commit is contained in:
@@ -710,7 +710,11 @@
|
||||
toAdd.push(model);
|
||||
this._addReference(model, options);
|
||||
}
|
||||
if (order) order.push(existing || model);
|
||||
|
||||
// Do not add multiple models with the same `id`.
|
||||
model = existing || model;
|
||||
if (order && (model.isNew() || !modelMap[model.id])) order.push(model);
|
||||
modelMap[model.id] = true;
|
||||
}
|
||||
|
||||
// Remove nonexistent models if appropriate.
|
||||
|
||||
@@ -1323,5 +1323,16 @@
|
||||
|
||||
});
|
||||
|
||||
test('Do not allow duplicate models to be `add`ed or `set`', function() {
|
||||
var c = new Backbone.Collection();
|
||||
|
||||
c.add([{id: 1}, {id: 1}]);
|
||||
equal(c.length, 1);
|
||||
equal(c.models.length, 1);
|
||||
|
||||
c.set([{id: 1}, {id: 1}]);
|
||||
equal(c.length, 1);
|
||||
equal(c.models.length, 1);
|
||||
});
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user