Skip to content

Commit

Permalink
merging in jashkenas#739 -- a massive simplification.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Nov 23, 2011
1 parent bc9fbcb commit 6687cde
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
24 changes: 14 additions & 10 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,15 @@

// Set a hash of model attributes on the object, firing `"change"` unless you
// choose to silence it.
set : function(attrs, options) {
set : function(key, value, options) {
var attrs;
if (_.isObject(key)) {
attrs = key;
options = value;
} else {
attrs = {};
attrs[key] = value;
}

// Extract attributes and options.
options || (options = {});
Expand All @@ -203,7 +211,7 @@
// Update attributes.
for (var attr in attrs) {
var val = attrs[attr];
if (!_.isEqual(now[attr], val) || options.unset && (attr in now)) {
if (!_.isEqual(now[attr], val) || (options.unset && (attr in now))) {
options.unset ? delete now[attr] : now[attr] = val;
delete escaped[attr];
this._changed = true;
Expand All @@ -221,20 +229,16 @@

// 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(attrs, options) {
if (_.isString(attrs)) {
var args = _.toArray(arguments), attrs = {};
while (_.isString(options = args.shift())) attrs[options] = void 0;
}
unset : function(attr, options) {
(options || (options = {})).unset = true;
return this.set(attrs, options);
return this.set(attr, null, options);
},

// Clear all attributes on the model, firing `"change"` unless you choose
// to silence it.
clear : function(options) {
var keys = _.without(_.keys(this.attributes), 'id');
return this.unset.apply(this, keys.concat([options]));
(options || (options = {})).unset = true;
return this.set(_.clone(this.attributes), options);
},

// Fetch the model from the server. If the server's representation of the
Expand Down
7 changes: 2 additions & 5 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ $(document).ready(function() {
ok(changeCount == 1, "Change count should NOT have incremented.");

a.validate = function(attrs) {
ok(attrs.foo === void 0, 'ignore values when unsetting');
equals(attrs.foo, void 0, 'ignore values when unsetting');
};
a.unset({foo: 1});
a.unset('foo');
ok(a.get('foo') == null, "Foo should have changed");
delete a.validate;
ok(changeCount == 2, "Change count should have incremented for unset.");
Expand Down Expand Up @@ -209,13 +209,10 @@ $(document).ready(function() {
model.bind("change", function() {
var changedAttrs = model.changedAttributes();
ok('name' in changedAttrs);
ok(!('id' in changedAttrs));
});
model.clear();
equals(changed, true);
equals(model.get('name'), undefined);
equals(model.id, 1);
equals(model.get('id'), 1);
});

test("Model: defaults", function() {
Expand Down

0 comments on commit 6687cde

Please sign in to comment.