diff --git a/backbone.js b/backbone.js index 5849d9cf..a44fce30 100644 --- a/backbone.js +++ b/backbone.js @@ -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]; }, diff --git a/test/model.js b/test/model.js index f30da6c6..feb24b8e 100644 --- a/test/model.js +++ b/test/model.js @@ -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); + }); + });