[Pkg-javascript-commits] [backbone] 180/211: Collection.add should fail if duplicate id/cid exists

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:00:21 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.5.0
in repository backbone.

commit 0853866aab703f4a2db373cd7c2ad329aa66e81f
Author: Niall Smart <niall at pobox.com>
Date:   Mon Jun 13 18:11:04 2011 -0700

    Collection.add should fail if duplicate id/cid exists
---
 backbone.js        |  2 +-
 test/collection.js | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/backbone.js b/backbone.js
index 6005ce0..8667e65 100644
--- a/backbone.js
+++ b/backbone.js
@@ -582,7 +582,7 @@
       options || (options = {});
       model = this._prepareModel(model, options);
       if (!model) return false;
-      var already = this.getByCid(model);
+      var already = this.getByCid(model) || this.get(model);
       if (already) throw new Error(["Can't add the same model to a set twice", already.id]);
       this._byId[model.id] = model;
       this._byCid[model.cid] = model;
diff --git a/test/collection.js b/test/collection.js
index dfbfbad..58daca2 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -95,6 +95,25 @@ $(document).ready(function() {
     equals(atCol.last(), h);
   });
 
+  test("Collection: add model to collection twice", function() {
+    try {
+      // same id, different cid
+      col.add({id: a.id, label: a.label});
+      ok(false, "duplicate; expected add to fail");
+    } catch (e) {
+      equals(e.message, "Can't add the same model to a set twice,3");
+    }
+    try {
+      // no id, same cid
+      var a2 = new Backbone.Model({label: a.label});
+      a2.cid = a.cid
+      col.add(a2);
+      ok(false, "duplicate; expected add to fail");
+    } catch (e) {
+      equals(e.message, "Can't add the same model to a set twice,3");
+    }
+  });
+
   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