Skip to content
This repository
Browse code

Much asked-for change. Instead of throwing an early error when adding…

… duplicate models, Backbone will simply skip them instead.
  • Loading branch information...
commit 18fba5772430f667c0d3cfb88d880039381c25fa 1 parent b1e2e69
Jeremy Ashkenas authored February 13, 2012
14  backbone.js
@@ -501,7 +501,7 @@
501 501
     // Add a model, or list of models to the set. Pass **silent** to avoid
502 502
     // firing the `add` event for every new model.
503 503
     add: function(models, options) {
504  
-      var i, index, length, model, cid, id, cids = {}, ids = {};
  504
+      var i, index, length, model, cid, id, cids = {}, ids = {}, dups = [];
505 505
       options || (options = {});
506 506
       models = _.isArray(models) ? models.slice() : [models];
507 507
 
@@ -513,14 +513,21 @@
513 513
         }
514 514
         if (cids[cid = model.cid] || this._byCid[cid] ||
515 515
           (((id = model.id) != null) && (ids[id] || this._byId[id]))) {
516  
-          throw new Error("Can't add the same model to a collection twice");
  516
+          dups.push(i);
  517
+          continue;
517 518
         }
518 519
         cids[cid] = ids[id] = model;
519 520
       }
520 521
 
  522
+      // Remove duplicates.
  523
+      i = dups.length;
  524
+      while (i--) {
  525
+        models.splice(dups[i], 1);
  526
+      }
  527
+
521 528
       // Listen to added models' events, and index models for lookup by
522 529
       // `id` and by `cid`.
523  
-      for (i = 0; i < length; i++) {
  530
+      for (i = 0, length = models.length; i < length; i++) {
524 531
         (model = models[i]).on('all', this._onModelEvent, this);
525 532
         this._byCid[model.cid] = model;
526 533
         if (model.id != null) this._byId[model.id] = model;
@@ -705,6 +712,7 @@
705 712
 
706 713
     // Prepare a model or hash of attributes to be added to this collection.
707 714
     _prepareModel: function(model, options) {
  715
+      options || (options = {});
708 716
       if (!(model instanceof Backbone.Model)) {
709 717
         var attrs = model;
710 718
         options.collection = this;
26  test/collection.js
@@ -124,22 +124,15 @@ $(document).ready(function() {
124 124
   });
125 125
 
126 126
   test("Collection: can't add model to collection twice", function() {
127  
-    raises(function(){
128  
-      // no id, same cid
129  
-      var a2 = new Backbone.Model({label: a.label});
130  
-      a2.cid = a.cid;
131  
-      col.push(a2);
132  
-      ok(false, "duplicate; expected add to fail");
133  
-    }, "Can't add the same model to a collection twice");
  127
+    var col = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]);
  128
+    equal(col.pluck('id').join(' '), '1 2 3');
134 129
   });
135 130
 
136 131
   test("Collection: can't add different model with same id to collection twice", function() {
137  
-    raises(function(){
138  
-      var col = new Backbone.Collection;
139  
-      col.unshift({id: 101});
140  
-      col.add({id: 101});
141  
-      ok(false, "duplicate; expected add to fail");
142  
-    }, "Can't add the same model to a collection twice");
  132
+    var col = new Backbone.Collection;
  133
+    col.unshift({id: 101});
  134
+    col.add({id: 101});
  135
+    equal(col.length, 1);
143 136
   });
144 137
 
145 138
   test("Collection: add model to multiple collections", function() {
@@ -518,8 +511,11 @@ $(document).ready(function() {
518 511
   test("Collection: multiple copies of the same model", function() {
519 512
     var col = new Backbone.Collection();
520 513
     var model = new Backbone.Model();
521  
-    raises(function() { col.add([model, model]); });
522  
-    raises(function() { col.add([{id: 1}, {id: 1}]); });
  514
+    col.add([model, model]);
  515
+    equal(col.length, 1);
  516
+    col.add([{id: 1}, {id: 1}]);
  517
+    equal(col.length, 2);
  518
+    equal(col.last().id, 1);
523 519
   });
524 520
 
525 521
   test("#964 - collection.get return in consistent", function() {

0 notes on commit 18fba57

Please sign in to comment.
Something went wrong with that request. Please try again.