[jQuery] Working on a getScripts plugin

[jQuery] Working on a getScripts plugin


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?
jQuery.extend({
    getScripts: function( files, callback ) {
        if( files.length && files[0] ) {
            var template = ['jQuery.getScript( "','','", function() {','','} );']
                , templateLast = ['jQuery.getScript( "','','", ','',' );']
                , FILE = 1, BODY = 3
                ;
            var stack = files.concat();
            templateLast[FILE] = stack.pop();
            templateLast[BODY] = callback.toString();
            var code = templateLast.join( "" );
            while( stack.length ) {
                var top = stack.pop();
                if( "" == top ) continue;
                template[FILE] = top;
                template[BODY] = code;
                code = template.join( "" );
            }
            jQuery.globalEval( code );
        }
    }
});
--
View this message in context: http://www.nabble.com/Working-on-a-getScripts-plugin-tf2696465.html#a7519511
Sent from the JQuery mailing list archive at Nabble.com.
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/