Skip to content

Commit

Permalink
Allow multiple names for one script. This accommodates the concatenat…
Browse files Browse the repository at this point in the history
…ion of scripts that provide different dependencies without changing the names referenced by the dependent functions.
  • Loading branch information
skeltoac committed Dec 1, 2010
1 parent 2ffc66c commit ac48f31
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/head.loader.js
Expand Up @@ -73,11 +73,21 @@
if (script) return script;

if (typeof url == 'object') {
for (var key in url) {
script = { name: key, url: url[key] };
if ( url.length > 1 ) {
script = { names: [] };
for (var key in url) {
if ( script.names.length == url.length - 1 )
script.url = url[key];
else
script.names.push(url[key]);
}
} else {
for (var key in url) {
script = { names: [ key ], url: url[key] };
}
}
} else {
script = { name: url.substring(url.indexOf("/", 10) + 1, url.indexOf("?")), url: url };
script = { names: [ url.substring(url.indexOf("/", 10) + 1, url.indexOf("?")) ], url: url };
}

scripts[script.url] = script;
Expand Down Expand Up @@ -176,9 +186,11 @@
//* console.info(" LOADED", script.name);

// waiters for this script
each(waiters[script.name], function(fn) {
fn.call();
});
for (var name in script.names) {
each(waiters[ script.names[name] ], function(fn) {
fn.call();
});
}

// TODO: do not run until DOM is loaded
var allLoaded = true;
Expand Down
3 changes: 3 additions & 0 deletions test/ab.js
@@ -0,0 +1,3 @@
a=1;
b=2;
console.log("loaded ab.js");
11 changes: 11 additions & 0 deletions test/names.html
@@ -0,0 +1,11 @@
<!doctype html>
<head>
<script src="../src/head.loader.js"></script>
<script>
head.ready("a", function(){console.log("a ready "+a);});
head.ready("b", function(){console.log("b ready "+b);});
head.js(["a", "b", "ab.js"]);
</script>
</head>
<body>
</body>

0 comments on commit ac48f31

Please sign in to comment.