Merge pull request #911 from braddunbar/validate

fixes #907 - `save` with `wait` succeeds without `validate`
This commit is contained in:
Jeremy Ashkenas
2012-01-31 07:29:49 -08:00
2 changed files with 19 additions and 14 deletions

View File

@@ -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 && 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;
}

View File

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