Skip to content

Commit

Permalink
Don't validate twice in
Browse files Browse the repository at this point in the history
  • Loading branch information
caseywebdev committed Mar 29, 2013
1 parent 8e7208e commit 4db2676
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
13 changes: 8 additions & 5 deletions backbone.js
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions test/model.js
Expand Up @@ -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});
Expand Down

0 comments on commit 4db2676

Please sign in to comment.