Hi,
> Some days ago I wrote this getScripts plugin, as a macro for shrinking this
>
> $.getScript( "f1.js", function() {
> $.getScript( "f2.js", function() {
> $.getScript( "f3.js", function() {
> do_it();
> } );
> } );
> } );
>
> to this
>
> $.getScripts( [ "f1.js", "f2.js", "f3.js" ],
> function( ) {
> do_it();
> } );
>
>
> I was wondering if I'm doing the right thing when converting the callback
> to a string. What do you think?
You can have that a lot cheaper. How about this:
jQuery.getScripts = function(scripts,callback) {
if( scripts.length == 0 ) {
callback();
return;
}
jQuery.getScript(scripts[0],function() {
jQuery.getScripts(scripts.slice(1),callback);
});
}
If the order you load the scripts is not important:
jQuery.getScripts = function(scripts,callback) {
var k = 0;
for( var i = 0; i < scripts.length; i++ )
jQuery.getScript(scripts[i],function() {
if( k++ == scripts.length ) callback();
});
}
That should call the callback as soon as the last script has finished to load.
The drawback of all of these solutins is that the loaded scripts have no
chance to load other scripts before the callback is executed. I have solved
that in jsPax (jspax.cdonat.de).
E.g. I do with jsPax:
$using(['jQuery.ajax','jQuery.event'],function() {
$('.fillme').click(function() { $(this).load('test.html'); });
});
The (slightly modified) jQuery.ajax and jQuery.event Modules see if jQuery
(also slightly modified) already has been loeded. If not it is loaed, then
jQuery.ajax and jQuery.event are executed and last but not least the callback
is called. That way I can be shure that jQuery.ajax and jQuery.event have all
they need before I use them.
Christof
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/