Fire 'change:attr' from change

This commit is contained in:
Brad Dunbar
2012-01-27 14:07:33 -05:00
parent 028771fcab
commit 0d6e5f3934
2 changed files with 12 additions and 8 deletions

View File

@@ -239,23 +239,16 @@
this._changing = true;
// Update attributes.
var changes = {};
for (attr in attrs) {
val = attrs[attr];
if (!_.isEqual(now[attr], val) || (options.unset && (attr in now))) {
delete escaped[attr];
this._changed = true;
changes[attr] = val;
}
options.unset ? delete now[attr] : now[attr] = val;
}
// Fire `change:attribute` events.
for (var attr in changes) {
if (!options.silent) this.trigger('change:' + attr, this, changes[attr], options);
}
// Fire the `"change"` event, if the model has been changed.
// Fire the `"change"` events, if the model has been changed.
if (!alreadyChanging) {
if (!options.silent && this._changed) this.change(options);
this._changing = false;
@@ -379,6 +372,10 @@
// Call this method to manually fire a `change` event for this model.
// Calling this will cause all objects observing the model to update.
change: function(options) {
var changes = this.changedAttributes();
for (var attr in changes) {
this.trigger('change:' + attr, this, changes[attr], options);
}
this.trigger('change', this, options);
this._previousAttributes = _.clone(this.attributes);
this._changed = false;

View File

@@ -552,4 +552,11 @@ $(document).ready(function() {
ok('x' in model.attributes);
});
test("change fires change:attr", 1, function() {
var model = new Backbone.Model({x: 1});
model.set({x: 2}, {silent: true});
model.on('change:x', function(){ ok(true); });
model.change();
});
});