[Pkg-javascript-commits] [backbone] 38/101: more tests, returning 'this', when performing side effects on collections

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 80acf9484d5a6faa848937c914454d770a83f0c4
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Wed Oct 6 13:36:28 2010 -0400

    more tests, returning 'this', when performing side effects on collections
---
 backbone.js        |  7 +++++--
 test/collection.js | 26 ++++++++++++++++++++++++++
 test/model.js      |  2 +-
 3 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/backbone.js b/backbone.js
index 91caaeb..c775e4e 100644
--- a/backbone.js
+++ b/backbone.js
@@ -382,8 +382,9 @@
         });
       }
       this._initialize();
-      this.add(models, true);
+      this.add(models, {silent: true});
       if (!options.silent) this.trigger('refresh');
+      return this;
     },
 
     // Fetch the default set of models for this collection, refreshing the
@@ -396,6 +397,7 @@
         if (options.success) options.success(collection, resp);
       };
       Backbone.request('GET', this, success, options.error);
+      return this;
     },
 
     // Create a new instance of a model in this collection.
@@ -408,7 +410,7 @@
         model.collection.add(model);
         if (options.success) options.success(model, resp);
       };
-      model.save(null, {success : success, error : options.error});
+      return model.save(null, {success : success, error : options.error});
     },
 
     // Force the set to re-sort itself. You don't need to call this under normal
@@ -418,6 +420,7 @@
       if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
       this.models = this.sortBy(this.comparator);
       if (!options.silent) this.trigger('refresh');
+      return this;
     },
 
     // Internal method called every time a model in the set fires an event.
diff --git a/test/collection.js b/test/collection.js
index 5e68472..96ddd04 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -2,6 +2,12 @@ $(document).ready(function() {
 
   module("Backbone collections");
 
+  window.lastRequest = null;
+
+  Backbone.request = function() {
+    lastRequest = _.toArray(arguments);
+  };
+
   var a = new Backbone.Model({id: 4, label: 'a'});
   var b = new Backbone.Model({id: 3, label: 'b'});
   var c = new Backbone.Model({id: 2, label: 'c'});
@@ -53,4 +59,24 @@ $(document).ready(function() {
     equals(col.first(), d);
   });
 
+  test("collections: refresh", function() {
+    var refreshed = 0;
+    var models = col.models;
+    col.bind('refresh', function() { refreshed += 1; });
+    col.refresh([]);
+    equals(refreshed, 1);
+    equals(col.length, 0);
+    equals(col.last(), null);
+    col.refresh(models);
+    equals(refreshed, 2);
+    equals(col.length, 4);
+    equals(col.last(), a);
+  });
+
+  test("collections: fetch", function() {
+    col.fetch();
+    equals(lastRequest[0], 'GET');
+    equals(lastRequest[1], col);
+  });
+
 });
diff --git a/test/model.js b/test/model.js
index 9d5e2a4..2e544bd 100644
--- a/test/model.js
+++ b/test/model.js
@@ -3,7 +3,7 @@ $(document).ready(function() {
   module("Backbone model");
 
   // Variable to catch the last request.
-  var lastRequest = null;
+  window.lastRequest = null;
 
   // Stub out Backbone.request...
   Backbone.request = function() {

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