Skip to content

Commit

Permalink
Issue 62: In head.ready(callback) the callback will be called immedia…
Browse files Browse the repository at this point in the history
…tely if all scripts are loaded.
  • Loading branch information
tipiirai committed Jan 14, 2011
1 parent e100326 commit 7814fa6
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions src/load.js
Expand Up @@ -48,15 +48,8 @@
el = getScript(el);
els.push(el);

load(el, fn && i == args.length -2 ? function() {
var allLoaded = true;

each(els, function(s) {

if (s.state != LOADED) { allLoaded = false; }
});

if (allLoaded) { fn(); }
load(el, fn && i == args.length -2 ? function() {
if (allLoaded(els)) { fn(); }

} : null);
}
Expand Down Expand Up @@ -109,20 +102,20 @@
}


api.ready = function(key, fn) {

var script = scripts[key];

if (script && script.state == LOADED) {
fn.call();
return api;
}
api.ready = function(key, fn) {

// shift arguments
if (isFunc(key)) {
fn = key;
key = "ALL";
}
}

var script = scripts[key];

if (script && script.state == LOADED || key == 'ALL' && allLoaded()) {
fn();
return api;
}

var arr = handlers[key];
if (!arr) { arr = handlers[key] = [fn]; }
Expand Down Expand Up @@ -184,6 +177,15 @@
return Object.prototype.toString.call(el) == '[object Function]';
}

function allLoaded(els) {
els = els || scripts;

for (var name in els) {
if (els[name].state != LOADED) { return false; }
}
return true;
}


function onPreload(script) {
script.state = PRELOADED;
Expand Down Expand Up @@ -233,18 +235,12 @@

// handlers for this script
each(handlers[script.name], function(fn) {
fn.call();
fn();
});

var allLoaded = true;

for (var name in scripts) {
if (scripts[name].state != LOADED) { allLoaded = false; }
}

if (allLoaded) {
if (allLoaded()) {
each(handlers.ALL, function(fn) {
if (!fn.done) { fn.call(); }
if (!fn.done) { fn(); }
fn.done = true;
});
}
Expand Down Expand Up @@ -279,9 +275,9 @@
setTimeout(function() {
ready = true;
each(queue, function(fn) {
fn.call();
fn();
});
}, 200);
}, 300);


// enable document.readyState for Firefox <= 3.5
Expand Down

0 comments on commit 7814fa6

Please sign in to comment.