[Pkg-javascript-commits] [backbone] 52/281: #673 - Adding index as a property on the options object that gets passed when the add/remove callbacks get triggered on a collection.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:01:56 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 80769fcf99ba47ad2d09ad0261db374c73917503
Author: Irene Ros <irene at bocoup.com>
Date:   Sat Oct 29 14:56:31 2011 -0400

    #673 - Adding index as a property on the options object that gets passed when the add/remove callbacks get triggered on a collection.
---
 backbone.js        |  5 ++++-
 test/collection.js | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/backbone.js b/backbone.js
index a4fd089..3d36166 100644
--- a/backbone.js
+++ b/backbone.js
@@ -608,6 +608,7 @@
       this.models.splice(index, 0, model);
       model.bind('all', this._onModelEvent);
       this.length++;
+      options.index = index;
       if (!options.silent) model.trigger('add', model, this, options);
       return model;
     },
@@ -620,8 +621,10 @@
       if (!model) return null;
       delete this._byId[model.id];
       delete this._byCid[model.cid];
-      this.models.splice(this.indexOf(model), 1);
+      var index = this.indexOf(model);
+      this.models.splice(index, 1);
       this.length--;
+      options.index = index;
       if (!options.silent) model.trigger('remove', model, this, options);
       this._removeReference(model);
       return model;
diff --git a/test/collection.js b/test/collection.js
index df44914..12ae311 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -95,6 +95,23 @@ $(document).ready(function() {
     equals(atCol.last(), h);
   });
 
+  test("Collection: add model to collection and verify index updates", function() {
+    var f = new Backbone.Model({id: 20, label : 'f'});
+    var g = new Backbone.Model({id: 21, label : 'g'});
+    var h = new Backbone.Model({id: 22, label : 'h'});
+    var col = new Backbone.Collection();
+    
+    var counts = [];
+     
+    col.bind('add', function(model, collection, options) {
+      counts.push(options.index);  
+    });
+    col.add(f); 
+    col.add(g); 
+    col.add(h); 
+    ok(_.isEqual(counts, [0,1,2]));
+  });
+
   test("Collection: add model to collection twice", function() {
     try {
       // no id, same cid
@@ -146,6 +163,23 @@ $(document).ready(function() {
     equals(otherRemoved, null);
   });
 
+  test("Collection: remove should return correct index events", function() {
+    var f = new Backbone.Model({id: 20, label : 'f'});
+    var g = new Backbone.Model({id: 21, label : 'g'});
+    var h = new Backbone.Model({id: 22, label : 'h'});
+    var col = new Backbone.Collection([f,g,h]);
+    
+    var counts = [];
+     
+    col.bind('remove', function(model, collection, options) {
+      counts.push(options.index);  
+    });
+    col.remove(h); 
+    col.remove(g); 
+    col.remove(f); 
+    ok(_.isEqual(counts, [2,1,0]));
+  });
+
   test("Collection: events are unbound on remove", function() {
     var counter = 0;
     var dj = new Backbone.Model();

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