Skip to content

Instantly share code, notes, and snippets.

@jboulhous
Created July 15, 2013 09:14
Show Gist options
  • Save jboulhous/5998577 to your computer and use it in GitHub Desktop.
Save jboulhous/5998577 to your computer and use it in GitHub Desktop.
beforeSync, afterSync for backbone Entities. https://github.com/eventray/backbone.synctracker
// https://github.com/eventray/backbone.synctracker
(function() {
'use strict';
//Save a reference to the global object (`window` in the browser, `exports`
// on the server).
var root = this;
var ajaxSync = Backbone.sync;
Backbone.sync = function(method, model, options, error) {
var me = this;
this.trigger('before:sync');
if (this.hasOwnProperty('beforeSync')) {
this.beforeSync();
}
this.fetched = false;
var deferred = ajaxSync.call(this, method, model, options, error);
deferred.then(function() {
me.fetched = true;
me.trigger('after:sync');
if (me.hasOwnProperty('afterSync')) {
me.afterSync();
}
});
return deferred;
}
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment