[Pkg-javascript-commits] [backbone] 273/281: missed an API regression in model.changedAttributes(), reimplementing in the same way as the original.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:21 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 4b4f2c1ad650e94a6d83fd7bd66e01311e159d20
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Mon Jan 30 12:18:58 2012 -0500

    missed an API regression in model.changedAttributes(), reimplementing in the same way as the original.
---
 backbone.js   | 17 ++++++++++++++---
 test/model.js |  7 +++++++
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/backbone.js b/backbone.js
index 2661972..b03b2c5 100644
--- a/backbone.js
+++ b/backbone.js
@@ -392,9 +392,20 @@
     // false if there are no changed attributes. Useful for determining what
     // parts of a view need to be updated and/or what attributes need to be
     // persisted to the server. Unset attributes will be set to undefined.
-    changedAttributes: function(now) {
-      if (!this.hasChanged()) return false;
-      return _.clone(this._changed);
+    // You can also pass an attributes object to diff against the model,
+    // determining if there *would be* a change.
+    changedAttributes: function(diff) {
+      var changed = false, old = this._previousAttributes;
+      if (diff) {
+        for (var attr in diff) {
+          if (_.isEqual(old[attr], diff[attr])) continue;
+          (changed || (changed = {}))[attr] = diff[attr];
+        }
+        return changed;
+      } else {
+        if (!this.hasChanged()) return false;
+        return _.clone(this._changed);
+      }
     },
 
     // Get the previous value of an attribute, recorded at the time the last
diff --git a/test/model.js b/test/model.js
index 9224a33..594fffa 100644
--- a/test/model.js
+++ b/test/model.js
@@ -279,6 +279,13 @@ $(document).ready(function() {
     equal(model.get('name'), 'Rob');
   });
 
+  test("Model: changedAttributes", function() {
+    var model = new Backbone.Model({a: 'a', b: 'b'});
+    equal(model.changedAttributes(), false);
+    equal(model.changedAttributes({a: 'a'}), false);
+    equal(model.changedAttributes({a: 'b'}).a, 'b');
+  });
+
   test("Model: change with options", function() {
     var value;
     var model = new Backbone.Model({name: 'Rob'});

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