public
Last active

asynchronous require() for JavaScript. Uses prototype.js.

  • Download Gist
require.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
var require = (function () {
var _required = {};
return (function (url, callback) {
if (typeof url == 'object') {
// we've (hopefully) got an array: time to chain!
if (url.length > 1) {
// load the nth file as soon as everything up to the n-1th one is done.
require(url.slice(0,url.length-1), function () {
require(url[url.length-1], callback);
});
} else if (url.length == 1) {
require(url[0], callback);
}
return;
}
if (typeof _required[url] == 'undefined') {
// haven't loaded this url yet; gogogo!
_required[url] = [];
 
var script = new Element('script', {src: url, type: 'text/javascript'});
script.observe('load', function () {
console.log("script " + url + " loaded.");
_required[url].each(function (cb) {
cb.call(); // TODO does this execute in the right context?
});
_required[url] = true;
});
 
$$('head')[0].insert(script);
} else if (typeof _required[url] == 'boolean') {
// we already loaded the thing, so go ahead
if (callback) { callback.call(); }
return;
}
 
if (callback) { _required[url].push(callback); }
});
})();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.