Skip to content

Instantly share code, notes, and snippets.

@kangax
Created January 29, 2013 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kangax/4666999 to your computer and use it in GitHub Desktop.
Save kangax/4666999 to your computer and use it in GitHub Desktop.
/**
* Usage:
*
* scout()
* .when(typeof JSON === 'undefined')
* .load('/assets/json2.js')
*
* .when(typeof G_vmlCanvasManager !== 'undefined')
* .load('/assets/fabric/cufon.js')
*
* .afterAll(function() {
* initEditor();
* });
*/
function scout() {
var testsToLoad = 0, tests = [ ];
var lastExpression;
var afterAllCallback;
function checkCompletion() {
if (testsToLoad === 0) {
afterAllCallback();
}
}
var self = {
when: function(val) {
lastExpression = val;
return self;
},
load: function(str) {
if (lastExpression) {
testsToLoad++;
tests.push(str);
}
return self;
},
afterAll: function(callback) {
afterAllCallback = callback;
checkCompletion();
for (var i = 0; i < tests.length; i++) {
jQuery.getScript(tests[i], function() {
testsToLoad--;
checkCompletion();
});
}
return self;
}
}
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment