Fixes #1938 -- Splits up 'error' into two events: 'error' (for XHR) and 'invalid' (for client side validation) errors.

This commit is contained in:
Jeremy Ashkenas
2012-12-19 11:17:35 -05:00
parent dd4b08154b
commit b76ca8338b
3 changed files with 13 additions and 7 deletions

View File

@@ -586,7 +586,7 @@
var error = this.validate(attrs, options);
if (!error) return true;
if (options && options.error) options.error(this, error, options);
this.trigger('error', this, error, options);
this.trigger('invalid', this, error, options);
return false;
}
@@ -641,7 +641,7 @@
// from being added.
for (i = models.length - 1; i >= 0; i--) {
if(!(model = this._prepareModel(models[i], options))) {
this.trigger("error", this, models[i], options);
this.trigger('invalid', this, models[i], options);
models.splice(i, 1);
continue;
}

View File

@@ -820,7 +820,8 @@ view.stopListening(model);
<li><b>"destroy"</b> (model, collection, options) &mdash; when a model is <a href="#Model-destroy">destroyed</a>. </li>
<li><b>"request"</b> (model, xhr, options) &mdash; when a model (or collection) has started a request to the server. </li>
<li><b>"sync"</b> (model, resp, options) &mdash; when a model has been successfully synced with the server. </li>
<li><b>"error"</b> (model, collection) &mdash; when a model's validation fails, or a <a href="#Model-save">save</a> call fails on the server. </li>
<li><b>"error"</b> (model, xhr, options) &mdash; when a model's <a href="#Model-save">save</a> call fails on the server. </li>
<li><b>"invalid"</b> (model, error, options) &mdash; when a model's <a href="#Model-validate">validation</a> fails on the client. </li>
<li><b>"route:[name]"</b> (params) &mdash; Fired by the router when a specific route is matched.</li>
<li><b>"route"</b> (router, route, params) &mdash; Fired by history when <i>any</i> route has been matched.</li>
<li><b>"all"</b> &mdash; this special event fires for <i>any</i> triggered event, passing the event name as the first argument. </li>
@@ -3895,6 +3896,11 @@ ActiveRecord::Base.include_root_in_json = false
been removed. Failed validations also trigger an error, even if an error
callback is specified in the options.
</li>
<li>
Consolidated <tt>"sync"</tt> and <tt>"error"</tt> events within
<a href="#Sync">Backbone.sync</a>. They are now triggered regardless
of the existence of <tt>success</tt> or <tt>error</tt> callbacks.
</li>
<li>
For mixed-mode APIs, <tt>Backbone.sync</tt> now accepts
<tt>emulateHTTP</tt> and <tt>emulateJSON</tt> as inline options.

View File

@@ -204,7 +204,7 @@ $(document).ready(function() {
a.validate = function(attrs) {
equal(attrs.foo, void 0, "validate:true passed while unsetting");
};
a.unset('foo', {validate:true});
a.unset('foo', {validate: true});
equal(a.get('foo'), void 0, "Foo should have changed");
delete a.validate;
ok(changeCount == 2, "Change count should have incremented for unset.");
@@ -442,7 +442,7 @@ $(document).ready(function() {
model.validate = function(attrs) {
if (attrs.admin != this.get('admin')) return "Can't change admin status.";
};
model.on('error', function(model, error) {
model.on('invalid', function(model, error) {
lastError = error;
});
var result = model.set({a: 100});
@@ -488,7 +488,7 @@ $(document).ready(function() {
var callback = function(model, error) {
lastError = error;
};
model.on('error', function(model, error) {
model.on('invalid', function(model, error) {
boundError = true;
});
var result = model.set({a: 100}, {error: callback, validate:true});
@@ -868,7 +868,7 @@ $(document).ready(function() {
validate: function(){ return 'invalid'; }
});
var model = new Model({id: 1});
model.on('error', function(){ ok(true); });
model.on('invalid', function(){ ok(true); });
model.save();
});