[Pkg-javascript-commits] [backbone] 152/281: only call splice once instead of on each add

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:06 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 1ffae591c84334e58cd73319af6a4a827cfcbe72
Author: Brad Dunbar <dunbarb2 at gmail.com>
Date:   Fri Jan 6 19:37:38 2012 -0500

    only call splice once instead of on each add
---
 backbone.js | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/backbone.js b/backbone.js
index 1234c06..85a74ce 100644
--- a/backbone.js
+++ b/backbone.js
@@ -15,8 +15,9 @@
   // Save the previous value of the `Backbone` variable.
   var previousBackbone = root.Backbone;
 
-  // Create a local reference to slice.
+  // Create a local reference to slice/splice.
   var slice = Array.prototype.slice;
+  var splice = Array.prototype.splice;
 
   // The top-level namespace. All public Backbone classes and modules will
   // be attached to this. Exported for both CommonJS and the browser.
@@ -420,14 +421,14 @@
       options || (options = {});
       if (!_.isArray(models)) models = [models];
       for (i = 0, l = models.length; i < l; i++) {
-        if (options && (options.at == +options.at) && i) options.at += 1;
         models[i] = this._add(models[i], options);
       }
+      i = options.at != null ? options.at : this.models.length;
+      splice.apply(this.models, [i, 0].concat(models));
       if (this.comparator) this.sort({silent: true});
-      if (!options.silent) {
-        for (i = 0; i < l; i++) {
-          models[i].trigger('add', models[i], this, options);
-        }
+      if (options.silent) return this;
+      for (i = 0; i < l; i++) {
+        models[i].trigger('add', models[i], this, options);
       }
       return this;
     },
@@ -568,11 +569,6 @@
       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;
-      if (options.at != null) {
-        this.models.splice(options.at, 0, model);
-      } else {
-        this.models.push(model);
-      }
       model.bind('all', this._onModelEvent);
       this.length++;
       return 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