diff --git a/backbone.js b/backbone.js index 6ee10c17..3048117e 100644 --- a/backbone.js +++ b/backbone.js @@ -206,8 +206,9 @@ }, // Remove an attribute from the model, firing `"change"` unless you choose - // to silence it. + // to silence it. `unset` is a noop if the attribute doesn't exist. unset : function(attr, options) { + if (!(attr in this.attributes)) return this; options || (options = {}); var value = this.attributes[attr]; diff --git a/test/model.js b/test/model.js index 649ed678..049db7f6 100644 --- a/test/model.js +++ b/test/model.js @@ -152,6 +152,17 @@ $(document).ready(function() { equals(a.id, undefined, "Unsetting the id should remove the id property."); }); + test("Model: multiple unsets", function() { + var i = 0; + var counter = function(){ i++; }; + var model = new Backbone.Model({a: 1}); + model.bind("change:a", counter); + model.set({a: 2}); + model.unset('a'); + model.unset('a'); + equals(i, 2, 'Unset does not fire an event for missing attributes.'); + }); + test("Model: using a non-default id attribute.", function() { var MongoModel = Backbone.Model.extend({idAttribute : '_id'}); var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});