update Ox.js loader

This commit is contained in:
rolux 2012-06-20 18:30:06 +02:00
parent e88f622449
commit 367a8227c9

View file

@ -1,27 +1,21 @@
'use strict'; 'use strict';
// Ox.js Developer Version (function(global) {
/* global.Ox = {
Usage:
Ox.load(callback)
Ox.load(module, callback)
Ox.load(module, options, callback)
Ox.load({module: options, module: options}, callback)
*/
window.Ox = {
load: function() { load: function() {
var args = arguments, var args = arguments,
callback = args[args.length - 1], callback = args[args.length - 1],
path = getPath(); path = getPath(),
time = +new Date();
loadJSON(function(data) { loadJSON(function(data) {
loadScripts(data.files, function() { loadScriptsSerial(data.files, function() {
Ox.VERSION = data.version; Ox.VERSION = data.version;
if (args.length == 1) { if (args.length == 1) {
console.log(document.body);
callback() callback()
} else { } else {
Ox.load.apply(null, args); Ox.load.apply(null, args);
@ -40,35 +34,20 @@ window.Ox = {
} }
function loadJSON(callback) { function loadJSON(callback) {
var req = new XMLHttpRequest(); var request = new XMLHttpRequest();
req.open( request.open('GET', path + 'Ox/json/Ox.json?' + time, true);
'GET', request.onreadystatechange = function() {
path + 'Ox/json/Ox.json?' + Math.floor(Math.random() * 1000000), if (request.readyState == 4) {
true if (request.status == 200) {
); callback(JSON.parse(request.responseText));
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
callback(JSON.parse(req.responseText));
} else {
// ...
} }
} }
}; };
req.send(); request.send();
} }
function loadScripts(scripts, callback) {
loadScriptsParallel(scripts[0], function() {
loadScriptsParallel(scripts[1], function() {
loadScriptsParallel(scripts[2], callback);
});
});
}
// fixme: unused
function loadScriptsSerial(scripts, callback) { function loadScriptsSerial(scripts, callback) {
loadScript(scripts.shift(), function() { loadScriptsParallel(scripts.shift(), function() {
if (scripts.length) { if (scripts.length) {
loadScriptsSerial(scripts, callback); loadScriptsSerial(scripts, callback);
} else { } else {
@ -78,9 +57,9 @@ window.Ox = {
} }
function loadScriptsParallel(scripts, callback) { function loadScriptsParallel(scripts, callback) {
var counter = 0, i, length = scripts.length; var counter = 0, length = scripts.length;
for (i = 0; i < length; i++) { while (scripts.length) {
loadScript(scripts[i], function() { loadScript(scripts.shift(), function() {
++counter == length && callback(); ++counter == length && callback();
}); });
} }
@ -88,18 +67,22 @@ window.Ox = {
function loadScript(script, callback) { function loadScript(script, callback) {
var element = document.createElement('script'), var element = document.createElement('script'),
head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement; head = document.head
|| document.getElementsByTagName('head')[0]
|| document.documentElement;
if (/MSIE/.test(navigator.userAgent)) { if (/MSIE/.test(navigator.userAgent)) {
// fixme: find a way to check if css/js have loaded in msie // FIXME: find a way to check if css/js have loaded in MSIE
setTimeout(callback, 2500); setTimeout(callback, 2500);
} else { } else {
element.onload = callback; element.onload = callback;
} }
element.src = path + script + '?' + Math.floor(Math.random() * 1000000); element.src = path + script + '?' + time;
element.type = 'text/javascript'; element.type = 'text/javascript';
head.appendChild(element); head.appendChild(element);
} }
} }
}; };
}(this));