1

I would like to start incorporating LABjs into my project but I want to know if there is anyway to conditionally load scripts. For example, something like:

$LAB
   .script('framework.js').wait()
   .script(function(){
       if(es){
          return  'es.js';
       }else{
          return '';
       }
   })

I have not tried this but am sure it will not work. Is there a more elegant way of doing this?

Thanks

Kyle Simpson
  • 15,725
  • 2
  • 33
  • 55
Adrian Adkison
  • 3,537
  • 5
  • 33
  • 36
  • To add some more concrete examples, localization scripts, browser specific scripts (gotta support the old ie6), scripts that are not needed if the user is authenticated. Im sure there are more. What do you do for these situations? – Adrian Adkison Aug 05 '10 at 18:43

3 Answers3

4

Actually, LABjs now (and for awhile) has supported conditional chaining... in a couple of ways. Firstly, you can pass a function to a .script() call, and that function can have conditional logic inside it that returns different URL strings (or nothing at all) depending on the conditions. Secondly, chains are now fully resumable (as of 2.0, being released in a few days), so you can do an easier form of the "simulated chaining" mentioned in that above blog post, by simply saving the state of a chain, and then picking up and adding to it later, like:

var $L = $LAB.script(...).script(...);

if (...) { $L = $L.script(...).wait(...);

// ...

$L = $L.script(...).....
Kyle Simpson
  • 15,725
  • 2
  • 33
  • 55
0

http://blog.getify.com/2010/02/simulated-chaining-in-javascript/

the answer ain't pretty but it's here

Adrian Adkison
  • 3,537
  • 5
  • 33
  • 36
0

I use LABjs for most of my projects, but when I need conditional chaining I use yepnode. It is fairly similar to LABjs, but I prefer how yepnodes handles conditional loading.

ashwoods
  • 2,209
  • 1
  • 22
  • 36