diff --git a/backbone.js b/backbone.js index 3922fb019..1890dc664 100644 --- a/backbone.js +++ b/backbone.js @@ -444,7 +444,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') { @@ -460,9 +460,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}`. diff --git a/test/model.js b/test/model.js index faede68a8..59680877b 100644 --- a/test/model.js +++ b/test/model.js @@ -959,10 +959,16 @@ $(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(); }, @@ -970,7 +976,13 @@ $(document).ready(function() { }); 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() {