Skip to content

Commit fa9a4c8

Browse files
committed
Passing through the options argument to 'change' events.
1 parent 160f0ba commit fa9a4c8

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

backbone.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
}
187187

188188
// Fire the `"change"` event, if the model has been changed.
189-
if (!options.silent && this._changed) this.change();
189+
if (!options.silent && this._changed) this.change(options);
190190
return this;
191191
},
192192

@@ -207,7 +207,7 @@
207207
if (!options.silent) {
208208
this._changed = true;
209209
this.trigger('change:' + attr, this);
210-
this.change();
210+
this.change(options);
211211
}
212212
return this;
213213
},
@@ -230,7 +230,7 @@
230230
for (attr in old) {
231231
this.trigger('change:' + attr, this);
232232
}
233-
this.change();
233+
this.change(options);
234234
}
235235
return this;
236236
},
@@ -309,8 +309,8 @@
309309

310310
// Call this method to manually fire a `change` event for this model.
311311
// Calling this will cause all objects observing the model to update.
312-
change : function() {
313-
this.trigger('change', this);
312+
change : function(options) {
313+
this.trigger('change', this, options);
314314
this._previousAttributes = _.clone(this.attributes);
315315
this._changed = false;
316316
},

test/model.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ $(document).ready(function() {
148148
equals(model.get('two'), null);
149149
});
150150

151-
test("Model: changed, hasChanged, changedAttributes, previous, previousAttributes", function() {
151+
test("Model: change, hasChanged, changedAttributes, previous, previousAttributes", function() {
152152
var model = new Backbone.Model({name : "Tim", age : 10});
153153
model.bind('change', function() {
154154
ok(model.hasChanged('name'), 'name changed');
@@ -162,6 +162,19 @@ $(document).ready(function() {
162162
equals(model.get('name'), 'Rob');
163163
});
164164

165+
test("Model: change with options", function() {
166+
var value;
167+
var model = new Backbone.Model({name: 'Rob'});
168+
model.bind('change', function(model, options) {
169+
value = options.prefix + model.get('name');
170+
});
171+
model.set({name: 'Bob'}, {silent: true});
172+
model.change({prefix: 'Mr. '});
173+
equals(value, 'Mr. Bob');
174+
model.set({name: 'Sue'}, {prefix: 'Ms. '});
175+
equals(value, 'Ms. Sue');
176+
});
177+
165178
test("Model: save within change event", function () {
166179
var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
167180
model.bind('change', function () {

0 commit comments

Comments
 (0)