LABjs conditionally loading scripts
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('fra开发者_如何转开发mework.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
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(...).....
http://blog.getify.com/2010/02/simulated-chaining-in-javascript/
the answer ain't pretty but it's here
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.
精彩评论