Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@digitalicarus
Last active August 29, 2015 13:57
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 digitalicarus/9443478 to your computer and use it in GitHub Desktop.
Save digitalicarus/9443478 to your computer and use it in GitHub Desktop.
(function () {
var w = window
, d = document
, b = d.body
, h = d.getElementsByTagName('head')[0]
, s = d.getElementsByTagName('script')
, c = {} // cache
, m // main
, i // iter
, x = /\.js$/
, x2= /(.*\/).*/
;
s = s[s.length-1];
m = s.getAttribute('data-main');
function l (src, cb, ctx) {
var j = d.createElement('script')
, src = (x.test(src)) ? src : src + '.js'
, ctx = ctx || src
;
j.src = src;
ctx && j.setAttribute('data-ctx', ctx);
j.onload = j.onreadystatechange = function() {
var r = this.readyState;
if ((!r || r == 'loaded' || r == 'complete')) {
j.onload = j.onreadystatechange = null;
//h.removeChild(j); // Handle memory leak in IE
cb && typeof cb === 'function' && cb(src);
}
};
h.appendChild(j);
}
w.define = function (deps, f) {
var n=0, s=d.querySelectorAll('head>script'), cid, ctx;
i=deps.length;
s=s[s.length-1];
cid=s.getAttribute('data-ctx') || s.src.replace(x,'');
if (i<0) {return;}
if (i===0) {
console.log('setting', cid);
c[cid] = f();
}
console.log('cid ' + cid + ' has deps', deps);
while(i--) {
n++;
l (s.src.replace(x2,'$1') + deps[i], function (src) {
console.log('just got ', src, 'for', cid);
if(--n===0) {
var args=[],i=deps.length;
while(i--) {
args.unshift(c[deps[i]]);
}
c[cid] = f.apply(null,args);
}
}, deps[i]);
}
};
w.c = c;
l(m);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment