Tuesday, September 20, 2011

Chaining scripts with LABjs

This article-guide on the use of LABjs may be simple or complex depending on how you approach.

Simple, if you're starting up or your scripts are located in external files and your chain of scripts are accessible from inside the <head> and are called the same from all over your scripts.

Complex, if you have otherwise and/or script blocks are inserted within or after the page. Changes can be all over.

The comment post of "Kevin Hakanson" provided the more simpler way to approach it, at least on how my projects are made : (script blocks can be anywhere you need them against putting all in one location and manipulating them there --- atomic approach may not always work). The method allows a single $LAB chain accross your pages, even if they are called separately or individually == define the instance of $LAB and use this variable in all independently called script blocks (taking his example):

  var instance = $LAB
.script("framework.js")
.script("myscript.js")
.wait(function(){ myscript.init(); });

instance.wait(function(){
framework.init();
framework.doSomething();
});

As you defined the variable as "instance", you can then call it on the other script blocks as "instance.script().wait()" chain to all script blocks.

No comments:

Post a Comment