mirror of
https://github.com/jashkenas/backbone.git
synced 2026-01-22 13:28:22 -05:00
passing falsey keys to hasChanged or previous
This commit is contained in:
@@ -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];
|
||||
},
|
||||
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user