Last active
August 29, 2015 14:25
-
-
Save fedyk/23761ce1236c5673fb84 to your computer and use it in GitHub Desktop.
Backbone Model fetch error, success and complete events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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