[Pkg-javascript-commits] [backbone] 169/281: Fixes #836, Fixes #708 -- going back to previous stance: two models with the same id can't be added to the same collection.
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 17:02:08 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.9.0
in repository backbone.
commit 8cfb243b8e46837c72f64811882d55c5e02cd569
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Wed Jan 11 17:03:30 2012 -0500
Fixes #836, Fixes #708 -- going back to previous stance: two models with the same id can't be added to the same collection.
---
backbone.js | 6 ++++--
test/collection.js | 13 ++++++++++++-
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/backbone.js b/backbone.js
index 5c7dae6..485ec30 100644
--- a/backbone.js
+++ b/backbone.js
@@ -422,10 +422,12 @@
models = slice.call(models);
for (i = 0, l = models.length; i < l; i++) {
var model = models[i] = this._prepareModel(models[i], options);
- if (this._byCid[model.cid]) {
+ var hasId = model.id != null;
+ if (this._byCid[model.cid] || (hasId && this._byId[model.id])) {
throw new Error("Can't add the same model to a set twice");
}
- this._byId[model.id] = this._byCid[model.cid] = model;
+ this._byCid[model.cid] = model;
+ if (hasId) this._byId[model.id] = model;
model.bind('all', this._onModelEvent, this);
this.length++;
}
diff --git a/test/collection.js b/test/collection.js
index 4275003..eecddb8 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -136,7 +136,7 @@ $(document).ready(function() {
}
});
- test("Collection: add model to collection twice", function() {
+ test("Collection: can't add model to collection twice", function() {
try {
// no id, same cid
var a2 = new Backbone.Model({label: a.label});
@@ -148,6 +148,17 @@ $(document).ready(function() {
}
});
+ test("Collection: can't add different model with same id to collection twice", function() {
+ var col = new Backbone.Collection;
+ try {
+ col.add({id: 101});
+ col.add({id: 101});
+ ok(false, "duplicate; expected add to fail");
+ } catch (e) {
+ equals(e.message, "Can't add the same model to a set twice");
+ }
+ });
+
test("Collection: add model to multiple collections", function() {
var counter = 0;
var e = new Backbone.Model({id: 10, label : 'e'});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git
More information about the Pkg-javascript-commits
mailing list