passing falsey keys to hasChanged or previous

This commit is contained in:
Brad Dunbar
2012-02-01 15:54:20 -05:00
parent c860070ca3
commit bc79feaf5a
2 changed files with 17 additions and 3 deletions

View File

@@ -384,8 +384,8 @@
// Determine if the model has changed since the last `"change"` event.
// If you specify an attribute name, determine if that attribute has changed.
hasChanged: function(attr) {
if (attr) return this._changed && _.has(this._changed, attr);
return !_.isEmpty(this._changed);
if (!arguments.length) return !_.isEmpty(this._changed);
return this._changed && _.has(this._changed, attr);
},
// Return an object containing all the attributes that have changed, or
@@ -407,7 +407,7 @@
// Get the previous value of an attribute, recorded at the time the last
// `"change"` event was fired.
previous: function(attr) {
if (!attr || !this._previousAttributes) return null;
if (!arguments.length || !this._previousAttributes) return null;
return this._previousAttributes[attr];
},

View File

@@ -589,4 +589,18 @@ $(document).ready(function() {
ok(lastRequest[1] === model);
});
test("`hasChanged` for falsey keys", function() {
var model = new Backbone.Model();
model.set({x: true}, {silent: true});
ok(!model.hasChanged(0));
ok(!model.hasChanged(''));
});
test("`previous` for falsey keys", function() {
var model = new Backbone.Model({0: true, '': true});
model.set({0: false, '': false}, {silent: true});
equal(model.previous(0), true);
equal(model.previous(''), true);
});
});