Skip to content
This repository
Browse code

Don't validate twice in

  • Loading branch information...
commit 4db26764779e942b9c02d114e9836508da800f44 1 parent 8e7208e
Casey Foster authored March 29, 2013

Showing 2 changed files with 17 additions and 5 deletions. Show diff stats Hide diff stats

  1. 13  backbone.js
  2. 9  test/model.js
13  backbone.js
@@ -455,11 +455,14 @@
455 455
 
456 456
       options = _.extend({validate: true}, options);
457 457
 
458  
-      // If we're not waiting and attributes exist, save acts as `set(attr).save(null, opts)`.
459  
-      if (attrs && !options.wait && !this.set(attrs, options)) return false;
460  
-
461  
-      // Do not persist invalid models.
462  
-      if (!this._validate(attrs, options)) return false;
  458
+      // If we're not waiting and attributes exist, save acts as
  459
+      // `set(attr).save(null, opts)` with validation. Otherwise, check if
  460
+      // the model will be valid when the attributes, if any, are set.
  461
+      if (attrs && !options.wait) {
  462
+        if (!this.set(attrs, options)) return false;
  463
+      } else {
  464
+        if (!this._validate(attrs, options)) return false;
  465
+      }
463 466
 
464 467
       // Set temporary attributes if `{wait: true}`.
465 468
       if (attrs && options.wait) {
9  test/model.js
@@ -712,6 +712,15 @@ $(document).ready(function() {
712 712
     equal(model.get('a'), void 0);
713 713
   });
714 714
 
  715
+  test("save doesn't validate twice", function () {
  716
+    var model = new Backbone.Model();
  717
+    var times = 0;
  718
+    model.sync = function () {};
  719
+    model.validate = function () { ++times; }
  720
+    model.save({});
  721
+    equal(times, 1);
  722
+  });
  723
+
715 724
   test("`hasChanged` for falsey keys", 2, function() {
716 725
     var model = new Backbone.Model();
717 726
     model.set({x: true}, {silent: true});

4 notes on commit 4db2676

Casey Foster
Collaborator

Whoops, definitely broke this commit message...

Tim Griesser
Collaborator

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

Casey Foster
Collaborator

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

Tim Griesser
Collaborator

My mistake - I read it wrong.

Please sign in to comment.
Something went wrong with that request. Please try again.