Skip to content

Instantly share code, notes, and snippets.

@dylanpyle
Created March 18, 2013 20:24
Show Gist options
  • Save dylanpyle/5190487 to your computer and use it in GitHub Desktop.
Save dylanpyle/5190487 to your computer and use it in GitHub Desktop.
Backbone.Sync with request queueing.
(function(){
var originalSync = Backbone.sync,
requestQueue = [];
var processQueue = function(){
var req = requestQueue.shift();
if (!req) return;
var sync = originalSync.call(Backbone, req.method, req.model, req.options);
sync.done(processQueue);
sync.fail(processQueue);
}
Backbone.sync = function(method, model, options) {
if(options.queue === true) {
requestQueue.push({
method: method,
model: model,
options: options
});
if(requestQueue.length === 1){
processQueue();
}
} else {
originalSync.call(Backbone, method, model, options);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment