[Pkg-javascript-commits] [backbone] 77/281: implement unset/clear in terms of set

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:01:58 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 368953eb3a9aa882aba085761e149d3ca10db975
Author: Brad Dunbar <dunbarb2 at gmail.com>
Date:   Thu Nov 10 08:54:59 2011 -0500

    implement unset/clear in terms of set
---
 backbone.js | 74 +++++++++++++------------------------------------------------
 1 file changed, 16 insertions(+), 58 deletions(-)

diff --git a/backbone.js b/backbone.js
index d5b0144..6cb805d 100644
--- a/backbone.js
+++ b/backbone.js
@@ -202,8 +202,8 @@
       // Update attributes.
       for (var attr in attrs) {
         var val = attrs[attr];
-        if (!_.isEqual(now[attr], val)) {
-          now[attr] = val;
+        if (!_.isEqual(now[attr], val) || options.unset && (attr in now)) {
+          options.unset ? delete now[attr] : now[attr] = val;
           delete escaped[attr];
           this._changed = true;
           if (!options.silent) this.trigger('change:' + attr, this, val, options);
@@ -220,53 +220,19 @@
 
     // Remove an attribute from the model, firing `"change"` unless you choose
     // to silence it. `unset` is a noop if the attribute doesn't exist.
-    unset : function(attr, options) {
-      if (!(attr in this.attributes)) return this;
-      options || (options = {});
-      var value = this.attributes[attr];
-
-      // Run validation.
-      var validObj = {};
-      validObj[attr] = void 0;
-      if (!options.silent && this.validate && !this._performValidation(validObj, options)) return false;
-
-      // changedAttributes needs to know if an attribute has been unset.
-      (this._unsetAttributes || (this._unsetAttributes = [])).push(attr);
-
-      // Remove the attribute.
-      delete this.attributes[attr];
-      delete this._escapedAttributes[attr];
-      if (attr == this.idAttribute) delete this.id;
-      this._changed = true;
-      if (!options.silent) {
-        this.trigger('change:' + attr, this, void 0, options);
-        this.change(options);
-      }
-      return this;
+    unset : function(attrs, options) {
+      var key = attrs;
+      if (_.isString(key)) (attrs = {})[key] = void 0;
+      (options || (options = {})).unset = true;
+      return this.set(attrs, options);
     },
 
     // Clear all attributes on the model, firing `"change"` unless you choose
     // to silence it.
     clear : function(options) {
-      options || (options = {});
-      var attr;
-      var old = this.attributes;
-
-      // Run validation.
-      var validObj = {};
-      for (attr in old) validObj[attr] = void 0;
-      if (!options.silent && this.validate && !this._performValidation(validObj, options)) return false;
-
-      this.attributes = {};
-      this._escapedAttributes = {};
-      this._changed = true;
-      if (!options.silent) {
-        for (attr in old) {
-          this.trigger('change:' + attr, this, void 0, options);
-        }
-        this.change(options);
-      }
-      return this;
+      var attrs = {};
+      for (var attr in this.attributes) if (attr != this.idAttribute) attrs[attr] = void 0;
+      return this.unset(attrs);
     },
 
     // Fetch the model from the server. If the server's representation of the
@@ -346,7 +312,6 @@
     change : function(options) {
       this.trigger('change', this, options);
       this._previousAttributes = _.clone(this.attributes);
-      this._unsetAttributes = null;
       this._changed = false;
     },
 
@@ -363,22 +328,15 @@
     // the server. Unset attributes will be set to undefined.
     changedAttributes : function(now) {
       now || (now = this.attributes);
-      var old = this._previousAttributes, unset = this._unsetAttributes;
-
-      var changed = false;
+      var changed = false, old = this._previousAttributes;
       for (var attr in now) {
-        if (!_.isEqual(old[attr], now[attr])) {
-          changed || (changed = {});
-          changed[attr] = now[attr];
-        }
+        if (_.isEqual(old[attr], now[attr])) continue;
+        (changed || (changed = {}))[attr] = now[attr];
       }
-
-      if (unset) {
-        changed || (changed = {});
-        var len = unset.length;
-        while (len--) changed[unset[len]] = void 0;
+      if (!this._changed) return changed;
+      for (var attr in old) {
+        if (!(attr in now)) (changed || (changed = {}))[attr] = void 0;
       }
-
       return changed;
     },
 

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