From fa9a4c879d9d43014eeb603b610c304a276fefbc Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 30 Nov 2010 15:35:43 -0500 Subject: [PATCH] Passing through the options argument to 'change' events. --- backbone.js | 10 +++++----- test/model.js | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/backbone.js b/backbone.js index 4ca830a7..a77ad53c 100644 --- a/backbone.js +++ b/backbone.js @@ -186,7 +186,7 @@ } // Fire the `"change"` event, if the model has been changed. - if (!options.silent && this._changed) this.change(); + if (!options.silent && this._changed) this.change(options); return this; }, @@ -207,7 +207,7 @@ if (!options.silent) { this._changed = true; this.trigger('change:' + attr, this); - this.change(); + this.change(options); } return this; }, @@ -230,7 +230,7 @@ for (attr in old) { this.trigger('change:' + attr, this); } - this.change(); + this.change(options); } return this; }, @@ -309,8 +309,8 @@ // 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() { - this.trigger('change', this); + change : function(options) { + this.trigger('change', this, options); this._previousAttributes = _.clone(this.attributes); this._changed = false; }, diff --git a/test/model.js b/test/model.js index b37b99a5..eb3efaed 100644 --- a/test/model.js +++ b/test/model.js @@ -148,7 +148,7 @@ $(document).ready(function() { equals(model.get('two'), null); }); - test("Model: changed, hasChanged, changedAttributes, previous, previousAttributes", function() { + test("Model: change, hasChanged, changedAttributes, previous, previousAttributes", function() { var model = new Backbone.Model({name : "Tim", age : 10}); model.bind('change', function() { ok(model.hasChanged('name'), 'name changed'); @@ -162,6 +162,19 @@ $(document).ready(function() { equals(model.get('name'), 'Rob'); }); + test("Model: change with options", function() { + var value; + var model = new Backbone.Model({name: 'Rob'}); + model.bind('change', function(model, options) { + value = options.prefix + model.get('name'); + }); + model.set({name: 'Bob'}, {silent: true}); + model.change({prefix: 'Mr. '}); + equals(value, 'Mr. Bob'); + model.set({name: 'Sue'}, {prefix: 'Ms. '}); + equals(value, 'Ms. Sue'); + }); + test("Model: save within change event", function () { var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"}); model.bind('change', function () {