Issue #209 ... unsetting a missing attribute should not fire a change event.

This commit is contained in:
Jeremy Ashkenas
2011-02-07 14:16:31 -05:00
parent 1a9404cfe8
commit b86d3f6afc
2 changed files with 13 additions and 1 deletions

View File

@@ -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];

View File

@@ -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'});