views:

14

answers:

0

I'm currently optimizing the javascripts used on two dozen sites (shared HTML templates), and one of the things I'm adding is dynamic loading of JQuery plugins. So far, I've got the following function (using an altered form of jQuery.getScript(), the default does not cache the fetched results):

function bootstrap(params) {
    if (params.requiredfunction) {
        callback();
    } else {
        $.getScript(params.liburl, params.callback);
    }
}

This is called as such:

bootstrap({
  'requiredfunction' : jQuery().fancybox,
  'callback' : Wpg.Fancybox.imageGallery,
  'liburl' : LIB_FANCYBOX
});

In my everlasting quest for optimization, there's a few things I'd like to ask if that's even possible.

First, each time the bootstrap is called, the callee has to add the URL of the library to fetch (in this case using a fake constant). However, as there's a direct relation between the required function and the library to download, I have the idea this is kinda unneeded. So, first question, is it possible to create a map (string -> string) based on a function or function name? I doubt I can use the function reference directly, and haven't found a cross-browser method to retrieve a function's name (to use it as a string index for the function map).

Second question, a bit more complex. Would it be possible to extend JQuery with a function with the same name of a function created by a plugin, have that function import the library, where the library overwrites the self-created function again? A pseudocode example (I don't actually know how to add a function to the jquery 'namespace'):

jQuery.somePluginFunction : function() {
   $.getScript('http://url-of-library', callback);
   // ...where in turn the somePluginFunction as defined by the external plugin is called (keeping the same parameters)
}

I have doubts the above is even possible, but might be someone had a bright idea or if it got someone thinking.

The first one, using a function or function name as a parameter in a map with function <-> library URL seems possible to me though. Note that I do not want to call the bootstrap() method using a string containing the function name - it's kinda tricky to call functions from a string, especially when those functions are in namespaces (or so I've read elsewhere).

/friday afternoon + beer question.