Skip to content

Instantly share code, notes, and snippets.

@corpix
Created March 3, 2012 15:07
Show Gist options
  • Save corpix/1966546 to your computer and use it in GitHub Desktop.
Save corpix/1966546 to your computer and use it in GitHub Desktop.
Backbone fetch multiple
var Utils = {};
/**
* Fetch multiple models(or collections) and execute passed callback
*
* @param {Array} stack - stack of objects
* @param {Function} callback - exec on ready
* @param {Object} ctx - callback context
*
* @returns {Object} context
*/
Utils.fetch = function(stack, callback, ctx){
var counter = stack.length
, error
, cb;
cb = function(){
counter--;
if(counter == 0){
return callback.call(ctx);
}
}
for(var i in stack){
var task = stack[i];
task.target.bind(task.event, cb).fetch();
}
return ctx;
}
var stack = [];
stack.push({ target: fooModel, event: 'reset' });
stack.push({ target: barModel, event: 'reset' });
Utils.fetch(stack, function(){
console.log('Models are ready');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment