Skip to content

Instantly share code, notes, and snippets.

@fedyk
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedyk/23761ce1236c5673fb84 to your computer and use it in GitHub Desktop.
Save fedyk/23761ce1236c5673fb84 to your computer and use it in GitHub Desktop.
Backbone Model fetch error, success and complete events
var ItemModel = Backbone.Model.extend({
// emit fetch:error, fetch:success, fetch:complete, and fetch:start events
fetch: function(options) {
var _this = this;
options = options || {};
var error = options.error;
var success = options.success;
var complete = options.complete;
options.error = function(xhr, textStatus, errorThrown) {
_this.trigger('fetch:error');
if (error) error(xhr, textStatus, errorThrown);
};
options.success = function(resp) {
_this.trigger('fetch:success');
if (success) success.call(options.context, resp);
};
options.complete = function() {
_this.trigger('fetch:complete');
if (complete) complete();
};
_this.trigger('fetch:start');
return Backbone.Model.prototype.fetch.call(this, options);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment