[Pkg-javascript-commits] [backbone] 202/281: Adding options.index to Collection#add and #remove.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:13 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 2dce41324775f2ad897693471748edcf03db7c8d
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Fri Jan 13 17:20:15 2012 -0500

    Adding options.index to Collection#add and #remove.
---
 backbone.js        | 20 +++++++++++++-------
 test/collection.js | 10 ++++++++--
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/backbone.js b/backbone.js
index 0e7cd63..24e9723 100644
--- a/backbone.js
+++ b/backbone.js
@@ -445,7 +445,7 @@
     // Add a model, or list of models to the set. Pass **silent** to avoid
     // firing the `added` event for every new model.
     add : function(models, options) {
-      var i, length;
+      var i, index, length;
       options || (options = {});
       if (!_.isArray(models)) models = [models];
       models = slice.call(models);
@@ -460,11 +460,12 @@
         model.on('all', this._onModelEvent, this);
       }
       this.length += length;
-      i = options.at != null ? options.at : this.models.length;
-      splice.apply(this.models, [i, 0].concat(models));
+      index = options.at != null ? options.at : this.models.length;
+      splice.apply(this.models, [index, 0].concat(models));
       if (this.comparator) this.sort({silent: true});
       if (options.silent) return this;
       for (i = 0; i < length; i++) {
+        options.index = index + i;
         models[i].trigger('add', models[i], this, options);
       }
       return this;
@@ -473,16 +474,21 @@
     // Remove a model, or a list of models from the set. Pass silent to avoid
     // firing the `removed` event for every model removed.
     remove : function(models, options) {
+      var i, index, model;
       options || (options = {});
       if (!_.isArray(models)) models = [models];
-      for (var i = 0, l = models.length; i < l; i++) {
-        var model = this.getByCid(models[i]) || this.get(models[i]);
+      for (i = 0, l = models.length; i < l; i++) {
+        model = this.getByCid(models[i]) || this.get(models[i]);
         if (!model) continue;
         delete this._byId[model.id];
         delete this._byCid[model.cid];
-        this.models.splice(this.indexOf(model), 1);
+        index = this.indexOf(model);
+        this.models.splice(index, 1);
         this.length--;
-        if (!options.silent) model.trigger('remove', model, this, options);
+        if (!options.silent) {
+          options.index = index;
+          model.trigger('remove', model, this, options);
+        }
         this._removeReference(model);
       }
       return this;
diff --git a/test/collection.js b/test/collection.js
index c0c8a79..691ada1 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -107,6 +107,7 @@ $(document).ready(function() {
     });
     col.bind('add', function(model, collection, options){
       added = model.get('label');
+      equals(options.index, 4);
       opts = options;
     });
     col.add(e, {amazing: true});
@@ -231,8 +232,13 @@ $(document).ready(function() {
 
   test("Collection: remove", function() {
     var removed = otherRemoved = null;
-    col.bind('remove', function(model){ removed = model.get('label'); });
-    otherCol.bind('remove', function(){ otherRemoved = true; });
+    col.bind('remove', function(model, col, options) {
+      removed = model.get('label');
+      equals(options.index, 4);
+    });
+    otherCol.bind('remove', function(model, col, options) {
+      otherRemoved = true;
+    });
     col.remove(e);
     equals(removed, 'e');
     equals(col.length, 4);

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