check for duplicate models/ids

This commit is contained in:
Brad Dunbar
2012-01-30 15:24:41 -05:00
parent fb97ef1e86
commit 50e8103fdf
2 changed files with 11 additions and 4 deletions

View File

@@ -470,7 +470,7 @@
// Add a model, or list of models to the set. Pass **silent** to avoid
// firing the `add` event for every new model.
add: function(models, options) {
var i, index, length, model, cids = {};
var i, index, length, model, cids = {}, ids = {};
options || (options = {});
models = _.isArray(models) ? models.slice() : [models];
@@ -480,10 +480,11 @@
if (!(model = models[i] = this._prepareModel(models[i], options))) {
throw new Error("Can't add an invalid model to a collection");
}
var hasId = model.id != null;
if (this._byCid[model.cid] || (hasId && this._byId[model.id])) {
if (cids[cid = model.cid] || this._byCid[cid] ||
(((id = model.id) != null) && (ids[id] || this._byId[id]))) {
throw new Error("Can't add the same model to a collection twice");
}
cids[cid] = ids[id] = model;
}
// Listen to added models' events, and index models for lookup by
@@ -492,7 +493,6 @@
(model = models[i]).on('all', this._onModelEvent, this);
this._byCid[model.cid] = model;
if (model.id != null) this._byId[model.id] = model;
cids[model.cid] = true;
}
// Insert models into the collection, re-sorting if needed, and triggering

View File

@@ -527,4 +527,11 @@ $(document).ready(function() {
equal(col.length, 0);
});
test("Collection: multiple copies of the same model", function() {
var col = new Backbone.Collection();
var model = new Backbone.Model();
raises(function() { col.add([model, model]) });
raises(function() { col.add([{id: 1}, {id: 1}]); });
});
});