[Pkg-javascript-commits] [backbone] 37/101: Making zero numeric ids more possible

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:58:27 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.1.0
in repository backbone.

commit 463ce3e62d4c80e20cf7ee15734dfa2e2bb1e10b
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Wed Oct 6 13:29:36 2010 -0400

    Making zero numeric ids more possible
---
 backbone.js        | 38 +++++++++++++++++++++-----------------
 test/collection.js | 16 +++++++++++++---
 2 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/backbone.js b/backbone.js
index c0fa477..91caaeb 100644
--- a/backbone.js
+++ b/backbone.js
@@ -299,7 +299,7 @@
 
     // Get a model from the set by id.
     get : function(id) {
-      return id && this._byId[id.id || id];
+      return id && this._byId[id.id != null ? id.id : id];
     },
 
     // Get a model from the set by client id.
@@ -322,16 +322,17 @@
       return this.models[index];
     },
 
-    // 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, silent) {
-      if (!_.isArray(models)) return this._add(models, silent);
-      for (var i=0; i<models.length; i++) this._add(models[i], silent);
+    // 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) {
+      if (!_.isArray(models)) return this._add(models, options);
+      for (var i=0; i<models.length; i++) this._add(models[i], options);
       return models;
     },
 
     // Internal implementation of adding a single model to the set.
-    _add : function(model, silent) {
+    _add : function(model, options) {
+      options || (options = {});
       var already = this.get(model);
       if (already) throw new Error(["Can't add the same model to a set twice", already.id]);
       this._byId[model.id] = model;
@@ -341,20 +342,21 @@
       this.models.splice(index, 0, model);
       model.bind('change', this._boundOnModelChange);
       this.length++;
-      if (!silent) this.trigger('add', model);
+      if (!options.silent) this.trigger('add', model);
       return model;
     },
 
     // 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, silent) {
-      if (!_.isArray(models)) return this._remove(models, silent);
-      for (var i=0; i<models.length; i++) this._remove(models[i], silent);
+    remove : function(models, options) {
+      if (!_.isArray(models)) return this._remove(models, options);
+      for (var i=0; i<models.length; i++) this._remove(models[i], options);
       return models;
     },
 
     // Internal implementation of removing a single model from the set.
-    _remove : function(model, silent) {
+    _remove : function(model, options) {
+      options || (options = {});
       model = this.get(model);
       if (!model) return null;
       delete this._byId[model.id];
@@ -363,14 +365,15 @@
       this.models.splice(this.indexOf(model), 1);
       model.unbind('change', this._boundOnModelChange);
       this.length--;
-      if (!silent) this.trigger('remove', model);
+      if (!options.silent) this.trigger('remove', model);
       return model;
     },
 
     // When you have more items than you want to add or remove individually,
     // you can refresh the entire set with a new list of models, without firing
     // any `added` or `removed` events. Fires `refreshed` when finished.
-    refresh : function(models, silent) {
+    refresh : function(models, options) {
+      options || (options = {});
       models = models || [];
       var collection = this;
       if (models[0] && !(models[0] instanceof Backbone.Model)) {
@@ -380,7 +383,7 @@
       }
       this._initialize();
       this.add(models, true);
-      if (!silent) this.trigger('refresh');
+      if (!options.silent) this.trigger('refresh');
     },
 
     // Fetch the default set of models for this collection, refreshing the
@@ -410,10 +413,11 @@
 
     // Force the set to re-sort itself. You don't need to call this under normal
     // circumstances, as the set will maintain sort order as each item is added.
-    sort : function(silent) {
+    sort : function(options) {
+      options || (options = {});
       if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
       this.models = this.sortBy(this.comparator);
-      if (!silent) this.trigger('refresh');
+      if (!options.silent) this.trigger('refresh');
     },
 
     // Internal method called every time a model in the set fires an event.
diff --git a/test/collection.js b/test/collection.js
index 2502b5e..5e68472 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -6,7 +6,8 @@ $(document).ready(function() {
   var b = new Backbone.Model({id: 3, label: 'b'});
   var c = new Backbone.Model({id: 2, label: 'c'});
   var d = new Backbone.Model({id: 1, label: 'd'});
-  var col = new Backbone.Collection([a,b,c,d]);
+  var e = null;
+  var col = window.col = new Backbone.Collection([a,b,c,d]);
 
   test("collections: simple and sorted", function() {
     equals(col.first(), a, "a should be first");
@@ -36,11 +37,20 @@ $(document).ready(function() {
   test("collections: add", function() {
     var added = null;
     col.bind('add', function(model){ added = model.get('label'); });
-    var e = new Backbone.Model({id: 0, label : 'e'});
+    e = new Backbone.Model({id: 0, label : 'e'});
     col.add(e);
     equals(added, 'e');
     equals(col.length, 5);
     equals(col.first(), e);
   });
 
-});
\ No newline at end of file
+  test("collections: remove", function() {
+    var removed = null;
+    col.bind('remove', function(model){ removed = model.get('label'); });
+    col.remove(e);
+    equals(removed, 'e');
+    equals(col.length, 4);
+    equals(col.first(), d);
+  });
+
+});

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