Permalink
Browse files

Don't validate twice in

  • Loading branch information...
1 parent 8e7208e commit 4db26764779e942b9c02d114e9836508da800f44 @caseywebdev caseywebdev committed Mar 29, 2013
Showing with 17 additions and 5 deletions.
  1. +8 −5 backbone.js
  2. +9 −0 test/model.js
View
@@ -455,11 +455,14 @@
options = _.extend({validate: true}, options);
- // If we're not waiting and attributes exist, save acts as `set(attr).save(null, opts)`.
- if (attrs && !options.wait && !this.set(attrs, options)) return false;
-
- // Do not persist invalid models.
- if (!this._validate(attrs, options)) return false;
+ // If we're not waiting and attributes exist, save acts as
+ // `set(attr).save(null, opts)` with validation. Otherwise, check if
+ // the model will be valid when the attributes, if any, are set.
+ if (attrs && !options.wait) {
+ if (!this.set(attrs, options)) return false;
+ } else {
+ if (!this._validate(attrs, options)) return false;
+ }
// Set temporary attributes if `{wait: true}`.
if (attrs && options.wait) {
View
@@ -712,6 +712,15 @@ $(document).ready(function() {
equal(model.get('a'), void 0);
});
+ test("save doesn't validate twice", function () {
+ var model = new Backbone.Model();
+ var times = 0;
+ model.sync = function () {};
+ model.validate = function () { ++times; }
+ model.save({});
+ equal(times, 1);
+ });
+
test("`hasChanged` for falsey keys", 2, function() {
var model = new Backbone.Model();
model.set({x: true}, {silent: true});

4 comments on commit 4db2676

Collaborator

caseywebdev replied Mar 30, 2013

Whoops, definitely broke this commit message...

Collaborator

tgriesser replied Mar 30, 2013

Doesn't this now skip validation on wait: true?

Collaborator

caseywebdev replied Mar 30, 2013

options.wait falls in the else validate, unless I'm not understanding your question.

Collaborator

tgriesser replied Mar 30, 2013

My mistake - I read it wrong.

Please sign in to comment.