Fixes #826 -- expose Backbone.wrapError.

This commit is contained in:
Jeremy Ashkenas
2012-01-12 16:05:53 -05:00
parent 4719659298
commit cee62e6541

View File

@@ -257,7 +257,7 @@
if (!model.set(model.parse(resp, xhr), options)) return false;
if (success) success(model, resp);
};
options.error = wrapError(options.error, model, options);
options.error = Backbone.wrapError(options.error, model, options);
return (this.sync || Backbone.sync).call(this, 'read', this, options);
},
@@ -273,7 +273,7 @@
if (!model.set(model.parse(resp, xhr), options)) return false;
if (success) success(model, resp, xhr);
};
options.error = wrapError(options.error, model, options);
options.error = Backbone.wrapError(options.error, model, options);
var method = this.isNew() ? 'create' : 'update';
return (this.sync || Backbone.sync).call(this, method, this, options);
},
@@ -289,7 +289,7 @@
model.trigger('destroy', model, model.collection, options);
if (success) success(model, resp);
};
options.error = wrapError(options.error, model, options);
options.error = Backbone.wrapError(options.error, model, options);
return (this.sync || Backbone.sync).call(this, 'delete', this, options);
},
@@ -523,7 +523,7 @@
collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
if (success) success(collection, resp);
};
options.error = wrapError(options.error, collection, options);
options.error = Backbone.wrapError(options.error, collection, options);
return (this.sync || Backbone.sync).call(this, 'read', this, options);
},
@@ -1062,6 +1062,18 @@
return $.ajax(_.extend(params, options));
};
// Wrap an optional error callback with a fallback error event.
Backbone.wrapError = function(onError, originalModel, options) {
return function(model, resp) {
var resp = model === originalModel ? resp : model;
if (onError) {
onError(model, resp, options);
} else {
originalModel.trigger('error', model, resp, options);
}
};
};
// Helpers
// -------
@@ -1119,16 +1131,4 @@
throw new Error('A "url" property or function must be specified');
};
// Wrap an optional error callback with a fallback error event.
var wrapError = function(onError, originalModel, options) {
return function(model, resp) {
var resp = model === originalModel ? resp : model;
if (onError) {
onError(model, resp, options);
} else {
originalModel.trigger('error', model, resp, options);
}
};
};
}).call(this);