Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed complete to add function to stack for per-set completes. Adde…

…d final call that runs all final callbacks. Made error like final in that is is now an array of callbacks. reduced timeout time from 25 to 15. Updated README.
  • Loading branch information...
commit f664051195b49e736cebd8905375c1f80b2b78c1 1 parent 8627745
@lemieuxster authored
View
59 Cigar.js
@@ -15,19 +15,17 @@
//Wait while paths are added to the queue
cigar_wait = null,
- //Paths imported
+ //Paths imported list
cigar_paths = {},
//Class path
cigar_classPath = "",
- //Complete function
- cigar_complete = function() {
- },
+ //Final function stack
+ cigar_final = [],
- //Fail function
- cigar_fail = function() {
- },
+ //Fail function stack
+ cigar_fail = [],
//Wait for script to load (5 seconds)
cigar_timeout = null,
@@ -51,7 +49,7 @@
if (cigar_wait) {
clearTimeout(cigar_wait);
}
- cigar_wait = setTimeout(cigar_start, 25);
+ cigar_wait = setTimeout(cigar_start, 15);
return Cigar;
},
@@ -66,7 +64,7 @@
if (q.length > 0) {
var nextQItem = q.shift();
if (typeof nextQItem === "function") {
- return cigar_runCallback(nextQItem);
+ return cigar_run_callback(nextQItem);
} else {
cigar_loadScript(nextQItem);
cigar_timeout = setTimeout(cigar_error, 5000);
@@ -77,7 +75,7 @@
}
} else {
cigar_isRunning = false;
- cigar_complete();
+ cigar_run_final();
}
}
@@ -95,7 +93,7 @@
},
//Run a callback method
- cigar_runCallback = function(callback) {
+ cigar_run_callback = function(callback) {
callback();
return cigar_next();
},
@@ -106,8 +104,26 @@
clearTimeout(cigar_timeout);
}
cigar_isRunning = false;
- cigar_fail();
+ cigar_run_fail();
return Cigar
+ },
+
+ //Final caller
+ cigar_run_final = function() {
+ for (var i = 0, l = cigar_final.length; i < l; ++i) {
+ if (typeof cigar_final[i] === "function") {
+ cigar_final[i]();
+ }
+ }
+ },
+
+ //Error caller
+ cigar_run_fail = function() {
+ for (var i = 0, l = cigar_fail.length; i < l; ++i) {
+ if (typeof cigar_fail[i] === "function") {
+ cigar_fail[i]();
+ }
+ }
};
//Create script tag, add to queue, return Cigar for chaining
@@ -142,7 +158,7 @@
cigar_paths[classPath] = scriptPath;
cigar_queue.push(tag);
if (!cigar_isRunning) {
- cigar_wait = setTimeout(cigar_start, 25);
+ cigar_wait = setTimeout(cigar_start, 15);
} else {
//interrupt
cigar_interrupt();
@@ -164,17 +180,22 @@
return Cigar;
};
- //Set the complete callback function
+ //Set a complete callback function and puts it on the stack
Cigar._complete = function(callbackFn) {
- //TODO make array, call last added (stack)
- cigar_complete = callbackFn;
+ var q = [callbackFn];
+ cigar_stack.unshift(q);
+ return Cigar;
+ };
+
+ //Push a final callback on the stack, run once all done
+ Cigar._final = function(callbackFn) {
+ cigar_final.unshift(callbackFn);
return Cigar;
};
- //Set the error callback function
+ //Set an error callback on the stack, run if there is an error
Cigar._error = function(failFn) {
- //TODO make array, loop through
- cigar_fail = failFn;
+ cigar_fail.unshift(failFn);
return Cigar;
};
View
10 README
@@ -19,14 +19,13 @@ this will import /path/to/js/fileName.js?203949 with the async attribute set.
//Chaining:
-You can also chain dependencies:
-//Hat tip to @jeffremer for the idea on this one.
+You can also chain dependencies -hat tip @jeffremer-:
_import("some.path.fileName").
_import("another.path.fileName").
_import("i.need.those.other.two").
_complete(function() {
- //called when all are loaded
+ //called when this set is loaded
});
You can also add inter-load callbacks like this:
@@ -35,7 +34,10 @@ _import("another.path.fileName").
_tap(function() { doSomethingHere(); }).
_import("i.need.those.other.two").
_complete(function() {
- //called when all are loaded
+ //called this set is loaded
+}).
+_final(function() {
+ //called at the end of all imports
});
View
7 example/CoolStuff.js
@@ -0,0 +1,7 @@
+var CoolStuff = {
+ amazing : function(id) {
+ var el = document.getElementById(id);
+ el.innerHTML = "This is SO COOL!";
+ el.style.display = "";
+ }
+};
View
5 example/INeedCoolStuff.js
@@ -0,0 +1,5 @@
+var CoolDependant = {
+ go : function() {
+ CoolStuff.amazing("manipulateMe");
+ }
+};
View
15 example/index.html
@@ -0,0 +1,15 @@
+<html>
+<head>
+ <script type="text/javascript" src="script/Cigar.js"></script>
+</head>
+<body>
+ <div id="manipulateMe" style="display: none;">Hey</div>
+ <script type="text/javascript">
+ _import("dev.js.cigar.script.CoolStuff").
+ _import("dev.js.cigar.script.INeedCoolStuff").
+ complete(function() {
+ CoolDependant.go();
+ });
+ </script>
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.