[Pkg-javascript-commits] [backbone] 20/101: fix bug in 'add' and 'refresh'

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:58:25 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 573d491f8d9b449d4a0ed0b61a3e2dc55bfed22f
Author: Joe Germuska <joe at germuska.com>
Date:   Sat Oct 2 17:16:17 2010 -0500

    fix bug in 'add' and 'refresh'
---
 backbone.js        | 18 ++++++++++++------
 test/collection.js | 19 +++++++++++++++++++
 test/model.js      | 11 ++++++++++-
 3 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/backbone.js b/backbone.js
index 437f637..6827578 100644
--- a/backbone.js
+++ b/backbone.js
@@ -284,9 +284,16 @@
   // Provides a standard collection class for our sets of models, ordered
   // or unordered. If a `comparator` is specified, the Collection will maintain
   // its models in sort order, as they're added and removed.
-  Backbone.Collection = function(options) {
+  Backbone.Collection = function(models,options) {
+    if (options && options.comparator) {
+      this.comparator = options.comparator;
+      delete options.comparator;
+    }
     this._boundOnModelEvent = _.bind(this._onModelEvent, this);
     this._initialize();
+    if (models) {
+      this.refresh(models,true);
+    }
   };
 
   // Define the Collection's inheritable methods.
@@ -340,7 +347,7 @@
       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;
-      var index = this.comparator ? this.sortedIndex(model, this.comparator) : this.length - 1;
+      var index = this.comparator ? this.sortedIndex(model, this.comparator) : this.length;
       this.models.splice(index, 0, model);
       model.bind('all', this._boundOnModelEvent);
       this.length++;
@@ -375,10 +382,9 @@
     refresh : function(models, silent) {
       models = models || [];
       if (models[0] && !(models[0] instanceof Backbone.Model)) {
-        for (var i = 0, l = models.length; i < l; i++) {
-          models[i].collection = this;
-          models[i] = new this.model(models[i]);
-        }
+        _.each(models, _.bind(function(model,i) {
+          model.collection = this;
+        }, this));
       }
       this._initialize();
       this.add(models, true);
diff --git a/test/collection.js b/test/collection.js
index e69de29..bc97695 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -0,0 +1,19 @@
+$(document).ready(function() {
+
+  module("Backbone collections");
+
+  test("collections: simple", function() {
+    a = new Backbone.Model({label: 'a'});
+    b = new Backbone.Model({label: 'b'});
+    c = new Backbone.Model({label: 'c'});
+    d = new Backbone.Model({label: 'd'});
+    col = new Backbone.Collection([a,b,c,d]);
+    equals(col.first(),a, "a should be first");
+    equals(col.last(),d, "d should be last");
+  });
+
+  test("collections: sorted", function() {
+      
+  });
+
+});
\ No newline at end of file
diff --git a/test/model.js b/test/model.js
index a836bc9..2804a5c 100644
--- a/test/model.js
+++ b/test/model.js
@@ -27,7 +27,9 @@ $(document).ready(function() {
   test("model: isNew", function() {
       attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
       a = new Backbone.Model(attrs);
-      ok(a.isNew());
+      ok(a.isNew(), "it should be new");
+      attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 };
+      ok(a.isNew(), "any defined ID is legal, negative or positive");
   })
 
   test("model: set", function() {
@@ -38,6 +40,13 @@ $(document).ready(function() {
       a.set({'foo': 2});
       ok(a.get('foo')==2, "Foo should have changed.");
       ok(changeCount == 1, "Change count should have incremented.");
+      a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
+      ok(a.get('foo')==2, "Foo should NOT have changed, still 2");
+      ok(changeCount == 1, "Change count should NOT have incremented.");
+      
+      a.unset('foo');
+      ok(a.get('foo')==null, "Foo should have changed");
+      ok(changeCount == 2, "Change count should have incremented for unset.");
      
   });
 

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