From 2dce41324775f2ad897693471748edcf03db7c8d Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 13 Jan 2012 17:20:15 -0500 Subject: [PATCH] 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 0e7cd639..24e9723b 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 c0c8a79a..691ada19 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);