Created
March 3, 2012 15:07
-
-
Save corpix/1966546 to your computer and use it in GitHub Desktop.
Backbone fetch multiple
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 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