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';
// Ox.js Developer Version
(function(global) {
/*
Usage:
Ox.load(callback)
Ox.load(module, callback)
Ox.load(module, options, callback)
Ox.load({module: options, module: options}, callback)
*/
window.Ox = {
global.Ox = {
load: function() {
var args = arguments,
callback = args[args.length - 1],
path = getPath();
path = getPath(),
time = +new Date();
loadJSON(function(data) {
loadScripts(data.files, function() {
loadScriptsSerial(data.files, function() {
Ox.VERSION = data.version;
if (args.length == 1) {
console.log(document.body);
callback()
} else {
Ox.load.apply(null, args);
@ -40,35 +34,20 @@ window.Ox = {
}
function loadJSON(callback) {
var req = new XMLHttpRequest();
req.open(
'GET',
path + 'Ox/json/Ox.json?' + Math.floor(Math.random() * 1000000),
true
);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
callback(JSON.parse(req.responseText));
} else {
// ...
var request = new XMLHttpRequest();
request.open('GET', path + 'Ox/json/Ox.json?' + time, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
callback(JSON.parse(request.responseText));
}
}
};
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) {
loadScript(scripts.shift(), function() {
loadScriptsParallel(scripts.shift(), function() {
if (scripts.length) {
loadScriptsSerial(scripts, callback);
} else {
@ -78,9 +57,9 @@ window.Ox = {
}
function loadScriptsParallel(scripts, callback) {
var counter = 0, i, length = scripts.length;
for (i = 0; i < length; i++) {
loadScript(scripts[i], function() {
var counter = 0, length = scripts.length;
while (scripts.length) {
loadScript(scripts.shift(), function() {
++counter == length && callback();
});
}
@ -88,14 +67,16 @@ window.Ox = {
function loadScript(script, callback) {
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)) {
// 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);
} else {
element.onload = callback;
}
element.src = path + script + '?' + Math.floor(Math.random() * 1000000);
element.src = path + script + '?' + time;
element.type = 'text/javascript';
head.appendChild(element);
}
@ -103,3 +84,5 @@ window.Ox = {
}
};
}(this));