[Pkg-javascript-commits] [backbone] 16/37: passing falsey keys to `hasChanged` or `previous`

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:47 UTC 2014


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

js pushed a commit to tag 0.9.1
in repository backbone.

commit bc79feaf5aa35daca709f8dc5dc0986410924bea
Author: Brad Dunbar <dunbarb2 at gmail.com>
Date:   Wed Feb 1 15:54:20 2012 -0500

    passing falsey keys to `hasChanged` or `previous`
---
 backbone.js   |  6 +++---
 test/model.js | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/backbone.js b/backbone.js
index 5849d9c..a44fce3 100644
--- a/backbone.js
+++ b/backbone.js
@@ -384,8 +384,8 @@
     // Determine if the model has changed since the last `"change"` event.
     // If you specify an attribute name, determine if that attribute has changed.
     hasChanged: function(attr) {
-      if (attr) return this._changed && _.has(this._changed, attr);
-      return !_.isEmpty(this._changed);
+      if (!arguments.length) return !_.isEmpty(this._changed);
+      return this._changed && _.has(this._changed, attr);
     },
 
     // Return an object containing all the attributes that have changed, or
@@ -407,7 +407,7 @@
     // Get the previous value of an attribute, recorded at the time the last
     // `"change"` event was fired.
     previous: function(attr) {
-      if (!attr || !this._previousAttributes) return null;
+      if (!arguments.length || !this._previousAttributes) return null;
       return this._previousAttributes[attr];
     },
 
diff --git a/test/model.js b/test/model.js
index f30da6c..feb24b8 100644
--- a/test/model.js
+++ b/test/model.js
@@ -589,4 +589,18 @@ $(document).ready(function() {
     ok(lastRequest[1] === model);
   });
 
+  test("`hasChanged` for falsey keys", function() {
+    var model = new Backbone.Model();
+    model.set({x: true}, {silent: true});
+    ok(!model.hasChanged(0));
+    ok(!model.hasChanged(''));
+  });
+
+  test("`previous` for falsey keys", function() {
+    var model = new Backbone.Model({0: true, '': true});
+    model.set({0: false, '': false}, {silent: true});
+    equal(model.previous(0), true);
+    equal(model.previous(''), true);
+  });
+
 });

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