Created
March 9, 2013 07:11
-
-
Save Mparaiso/5123277 to your computer and use it in GitHub Desktop.
Charge.js : simple script loader
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
/** | |
* @module Charge | |
* @author M.Paraiso | |
*/ | |
(function(ns){ | |
"use strict"; | |
var C = {}; | |
C.getXmlHttp = function(method,callback){ | |
var xmlHttp = new XMLHttpRequest(); | |
xmlHttp.onreadystatechange(function(event){ | |
if(event.status==200 && xmlHttp.readyState==4){ | |
callback(xmlHttp.responseText); | |
} | |
}); | |
return xmlHttp; | |
} | |
C.request=function(type,method,url,success,error){ | |
var r; | |
switch(type){ | |
case "script": | |
r =document.createElement("SCRIPT"); | |
r.src = url; | |
r.onload=success; | |
r.onerror=error; | |
document.head.appendChild(r); | |
break; | |
default: | |
throw "type must be specified"; | |
} | |
}; | |
C.load = function(scriptCollection,callback){ | |
var DO = function(scriptCollection){ | |
var current; | |
C.request("script",null,current = scriptCollection.shift(),function(){ | |
if(scriptCollection.length>0){ | |
return DO(scriptCollection); | |
}else{ | |
return callback(); | |
} | |
},function(){ | |
throw "".concat("failed to load ",current); | |
}); | |
}; | |
if(scriptCollection.length>0){ | |
return DO(scriptCollection); | |
} | |
}; | |
ns.Charge = C; | |
/** | |
* @note @requirejs rendre un module compatible avec requirejs et amd | |
*/ | |
if(typeof module!=undefined && typeof exports !=undefined ){ | |
module.exports = C; | |
} | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment