Skip to content

Instantly share code, notes, and snippets.

@devth
Created February 8, 2012 19:24
Show Gist options
  • Save devth/1772520 to your computer and use it in GitHub Desktop.
Save devth/1772520 to your computer and use it in GitHub Desktop.
ns("decide").ApplicationModel = Backbone.Model.extend({
sync: function(method, model, options) {
// Our overriden sync supports these events:
// sync:start
// sync:success
// sync:error
// sync:complete
//
// Params dispatched with the event:
// method, model, status
//
this.trigger('sync:start', method, model);
this.isLoading = true;
options = options || {};
var complete = function(status) { model.trigger('sync:complete', method, model, status);
model.isLoading = false; };
var modifiedOpts = {
success: function(resp, status, xhr) {
model.trigger('sync:success', method, model, status);
complete(status);
if (_.has(options, 'success')) { options.success(resp, status, xhr); }
},
error: function(resp, status, xhr) {
model.trigger('sync:error', method, model, status);
complete(status);
if (_.has(options, 'error')) { options.error(resp, status, xhr); }
}
};
// Use Backbone's native sync to finish the job
Backbone.sync.call(this, method, model, modifiedOpts);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment