Skip to content

Instantly share code, notes, and snippets.

@bverbeken
Created April 18, 2013 15:11
Show Gist options
  • Save bverbeken/5413498 to your computer and use it in GitHub Desktop.
Save bverbeken/5413498 to your computer and use it in GitHub Desktop.
Poor man's requirejs
; (function($, Raphael, window, document, undefined){
function loadLibs(libs, callback) {
function loadScripts(callback) {
libs.forEach(function(lib){
if (!lib.getLib()){
console.log("loading " + lib.url);
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = 'async';
script.src = lib.url;
head.appendChild(script);
}
});
checkReady(callback);
}
function checkReady (callback) {
if (allLibsLoaded()){
callback();
} else {
window.setTimeout(function() { checkReady(callback); }, 100);
}
};
function allLibsLoaded(){
for (var i = 0; i < libs.length; i++) {
if (!libs[i].getLib()) return false;
}
return true;
}
loadScripts(callback);
}
loadLibs(
[
{getLib: function(){return window.jQuery;}, url: "http://code.jquery.com/jquery-1.9.1.min.js"},
{getLib: function(){return window.Raphael;}, url: "https://raw.github.com/DmitryBaranovskiy/raphael/master/raphael-min.js"}
],
function(){
$ = window.jQuery.noConflict();
Raphael = window.Raphael;
console.log("jquery: " + $().jquery);
console.log("Raphael: " + Raphael);
$("#bla").html("everything is loaded! ");
}
);
}(typeof jQuery != 'undefined' ? jQuery : undefined, typeof Raphael != 'undefined' ? Raphael : undefined, window, document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment