Skip to content

Instantly share code, notes, and snippets.

@getify
Created November 17, 2010 22:20
Show Gist options
  • Save getify/704226 to your computer and use it in GitHub Desktop.
Save getify/704226 to your computer and use it in GitHub Desktop.
adding "queueing" to LABjs dynamically, to be able to simulate the $LAB chain dynamically with a for-loop
<!DOCTYPE html>
<html>
<head>
<title>LABjs Demo</title>
<script src="/js/queue.LAB.js"></script>
<script>
// adding `false` in the queue will signifiy an empty .wait() in the $LAB chain
$LAB.queue("http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js", false);
$LAB.queue("http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js", false);
if (_is_IE) $LAB.queue("/js/ie-only.js");
</script>
</head>
<body>
<!-- other stuff -->
<script>
$LAB.queue("/js/mypage1.js");
</script>
<!-- more stuff -->
<script>
$LAB.queue("/js/myinit.js",function(){
$(document).ready(function(){
myinit();
// other stuff
});
});
</script>
<!-- still more stuff -->
<script>
$LAB.executeQueue();
</script>
</body>
</html>
/*! LAB.js v1.2.0 (c) Kyle Simpson MIT License */
/* INCLUDE THE MINIFIED LAB.js CODE HERE */
// add some boilerplate "queue" behavior for use with LABjs
$LAB._queue = [];
$LAB.queue = function() {
Array.prototype.push.apply($LAB._queue,arguments);
return this;
};
$LAB.executeQueue = function() {
var $L = $LAB;
for (var i=0, len=$LAB._queue.length; i<len; i++) {
if (typeof $LAB._queue[0] == "string") {
$L = $L.script($LAB._queue[0]);
}
else if ($LAB._queue[0] === false) {
$L = $L.wait();
}
else {
$L = $L.wait($LAB._queue[0]);
}
$LAB._queue.shift(); // remove first element from the _queue
}
$LAB._queue = [];
};
@rmaksim
Copy link

rmaksim commented Nov 22, 2010

$LAB.queue = function() {
Array.prototype.push.apply($LAB._queue,arguments); // !!!
return this; // !!!
};

if (typeof $LAB._queue[0] == "string") {
$L = $L.script($LAB._queue[0]); // !!! [0] instead of [i]
}

@getify
Copy link
Author

getify commented Nov 22, 2010

@rmaksim -- good catches... this was a hastily written snippet while I was giving my #jqsummit talk. :) I've updated the code accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment