Skip to content

Commit

Permalink
fix jashkenas#2345 return a rejected promise instead of false on inva…
Browse files Browse the repository at this point in the history
…lid model save
  • Loading branch information
akre54 committed Jun 6, 2013
1 parent f69f1e6 commit deff340
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
6 changes: 3 additions & 3 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@
// If the server returns an attributes hash that differs, the model's
// state will be `set` again.
save: function(key, val, options) {
var attrs, method, xhr, attributes = this.attributes;
var attrs, method, xhr, dfd = new $.Deferred, attributes = this.attributes;

// Handle both `"key", value` and `{key: value}` -style arguments.
if (key == null || typeof key === 'object') {
Expand All @@ -461,9 +461,9 @@
// `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;
if (!this.set(attrs, options)) return dfd.rejectWith(this);
} else {
if (!this._validate(attrs, options)) return false;
if (!this._validate(attrs, options)) return dfd.rejectWith(this);
}

// Set temporary attributes if `{wait: true}`.
Expand Down
18 changes: 15 additions & 3 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,18 +959,30 @@ $(document).ready(function() {
var model = new Backbone.Model;
model.validate = function(){ return 'invalid'; };
model.sync = function(){ ok(false); };
strictEqual(model.save(), false);
model.save()
.done(function() {
ok(false);
})
.fail(function() {
ok(true);
});
});

test("#1377 - Save without attrs triggers 'error'.", 1, function() {
test("#1377 - Save without attrs triggers 'error'.", 2, function() {
var Model = Backbone.Model.extend({
url: '/test/',
sync: function(method, model, options){ options.success(); },
validate: function(){ return 'invalid'; }
});
var model = new Model({id: 1});
model.on('invalid', function(){ ok(true); });
model.save();
model.save()
.done(function() {
ok(false);
})
.fail(function() {
ok(true);
});
});

test("#1545 - `undefined` can be passed to a model constructor without coersion", function() {
Expand Down

0 comments on commit deff340

Please sign in to comment.