mirror of
https://github.com/jashkenas/backbone.git
synced 2026-01-23 05:48:10 -05:00
first draft of REST-failure 'error' events.
This commit is contained in:
19
backbone.js
19
backbone.js
@@ -245,7 +245,7 @@
|
||||
if (!model.set(model.parse(resp), options)) return false;
|
||||
if (options.success) options.success(model, resp);
|
||||
};
|
||||
var error = options.error && _.bind(options.error, null, model);
|
||||
var error = wrapError(options.error, model);
|
||||
(this.sync || Backbone.sync)('read', this, success, error);
|
||||
return this;
|
||||
},
|
||||
@@ -261,7 +261,7 @@
|
||||
if (!model.set(model.parse(resp), options)) return false;
|
||||
if (options.success) options.success(model, resp);
|
||||
};
|
||||
var error = options.error && _.bind(options.error, null, model);
|
||||
var error = wrapError(options.error, model);
|
||||
var method = this.isNew() ? 'create' : 'update';
|
||||
(this.sync || Backbone.sync)(method, this, success, error);
|
||||
return this;
|
||||
@@ -276,7 +276,7 @@
|
||||
if (model.collection) model.collection.remove(model);
|
||||
if (options.success) options.success(model, resp);
|
||||
};
|
||||
var error = options.error && _.bind(options.error, null, model);
|
||||
var error = wrapError(options.error, model);
|
||||
(this.sync || Backbone.sync)('delete', this, success, error);
|
||||
return this;
|
||||
},
|
||||
@@ -483,7 +483,7 @@
|
||||
collection.refresh(collection.parse(resp));
|
||||
if (options.success) options.success(collection, resp);
|
||||
};
|
||||
var error = options.error && _.bind(options.error, null, collection);
|
||||
var error = wrapError(options.error, collection);
|
||||
(this.sync || Backbone.sync)('read', this, success, error);
|
||||
return this;
|
||||
},
|
||||
@@ -993,6 +993,17 @@
|
||||
return _.isFunction(object.url) ? object.url() : object.url;
|
||||
};
|
||||
|
||||
// Wrap an optional error callback with a fallback error event.
|
||||
var wrapError = function(onError, model) {
|
||||
return function(resp) {
|
||||
if (onError) {
|
||||
onError(model, resp);
|
||||
} else {
|
||||
model.trigger('error', model, resp);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Helper function to escape a string for HTML rendering.
|
||||
var escapeHTML = function(string) {
|
||||
return string.replace(/&(?!\w+;)/g, '&')
|
||||
|
||||
Reference in New Issue
Block a user