From db95e2c1fb9846a713685d35b12c9c4ce91d58f1 Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Tue, 31 Jan 2012 09:46:23 -0500 Subject: [PATCH 1/2] fixes #907 - `save` with `wait` succeeds without `validate` --- backbone.js | 27 +++++++++++++-------------- test/model.js | 6 ++++++ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/backbone.js b/backbone.js index d6a0aa9e..bca4595a 100644 --- a/backbone.js +++ b/backbone.js @@ -226,7 +226,7 @@ if (options.unset) for (attr in attrs) attrs[attr] = void 0; // Run validation. - if (this.validate && !this._performValidation(attrs, options)) return false; + if (!this._validate(attrs, options)) return false; // Check for changes of `id`. if (this.idAttribute in attrs) this.id = attrs[this.idAttribute]; @@ -299,7 +299,7 @@ } options = options ? _.clone(options) : {}; - if (attrs && !this[options.wait ? '_performValidation' : 'set'](attrs, options)) return false; + if (attrs && !this[options.wait ? '_validate' : 'set'](attrs, options)) return false; var model = this; var success = options.success; options.success = function(resp, status, xhr) { @@ -420,18 +420,17 @@ // Run validation against a set of incoming attributes, returning `true` // if all is well. If a specific `error` callback has been passed, // call that instead of firing the general `"error"` event. - _performValidation: function(attrs, options) { - var newAttrs = _.extend({}, this.attributes, attrs); - var error = this.validate(newAttrs, options); - if (error) { - if (options.error) { - options.error(this, error, options); - } else { - this.trigger('error', this, error, options); - } - return false; + _validate: function(attrs, options) { + if (!_.isFunction(this.validate)) return true; + attrs = _.extend({}, this.attributes, attrs); + var error = this.validate(attrs, options); + if (!error) return true; + if (options.error) { + options.error(this, error, options); + } else { + this.trigger('error', this, error, options); } - return true; + return false; } }); @@ -650,7 +649,7 @@ var attrs = model; options.collection = this; model = new this.model(attrs, options); - if (model.validate && !model._performValidation(model.attributes, options)) model = false; + if (!model._validate(model.attributes, options)) model = false; } else if (!model.collection) { model.collection = this; } diff --git a/test/model.js b/test/model.js index 594fffa0..f30da6c6 100644 --- a/test/model.js +++ b/test/model.js @@ -583,4 +583,10 @@ $(document).ready(function() { ok(model.hasChanged()); }); + test("save with `wait` succeeds without `validate`", function() { + var model = new Backbone.Model(); + model.save({x: 1}, {wait: true}); + ok(lastRequest[1] === model); + }); + }); From 7a486392f1b7af4bc1050e2b71490a4e9274d952 Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Tue, 31 Jan 2012 10:21:56 -0500 Subject: [PATCH 2/2] ensure `options` is present --- backbone.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backbone.js b/backbone.js index bca4595a..f5ab94d9 100644 --- a/backbone.js +++ b/backbone.js @@ -425,7 +425,7 @@ attrs = _.extend({}, this.attributes, attrs); var error = this.validate(attrs, options); if (!error) return true; - if (options.error) { + if (options && options.error) { options.error(this, error, options); } else { this.trigger('error', this, error, options);