Fix #2976 Do not add multiple models with same id

This commit is contained in:
Casey Foster
2014-02-03 13:19:27 -06:00
parent 8d4b1967fe
commit 990ab04a45
2 changed files with 16 additions and 1 deletions

View File

@@ -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.

View File

@@ -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);
});
})();