mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-30 03:00:06 -04:00
merging in #739 -- a massive simplification.
This commit is contained in:
24
backbone.js
24
backbone.js
@@ -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 = {});
|
||||
@@ -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;
|
||||
@@ -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
|
||||
|
||||
@@ -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.");
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user