Skip to content

Commit

Permalink
implement unset/clear in terms of set
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Nov 10, 2011
1 parent 3f00419 commit 368953e
Showing 1 changed file with 16 additions and 58 deletions.
74 changes: 16 additions & 58 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -346,7 +312,6 @@
change : function(options) {
this.trigger('change', this, options);
this._previousAttributes = _.clone(this.attributes);
this._unsetAttributes = null;
this._changed = false;
},

Expand All @@ -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;
},

Expand Down

0 comments on commit 368953e

Please sign in to comment.