Hi,

> > Why not use OpenAjax?
>
> http://ejohn.org/blog/thoughts-on-openajax/

OK, I accept the legal stuff. I did not join that Alliance because I did not 
understand any of that. I have problems with legal texts in german - a 
foreign language doesn't make that better.

The technical stuff: OpenAjax is a specification and not an implementation. 
The refference implementation is in fact really bad. The mistake I see in 
Johns analysis as well as in deans comment in the jQuery blog is, that they 
analyse the poor code of the refference imlementation and take that to 
condemn the specification. I think the spec could be a lot worse.

As I said I was planing to write an OpenAjax Hub as a jsPax package. I took 
the refference implementation as a place to look for the desired behaviour - 
not as base for the code.

> > If you would like to load your js files that way as well, your API looks
> > much like the jsPax API. I was planing to make an "OpenAjax"-package for
> > jsPax that uses the jsPax package information, but did not get around to
> > do it.
>
> The idea was to give users of plugin a hint which dependencies are not
> loaded yet, without any fancy packaging on the clientside.

Actually I was falling in the naming trap that Brandon already noted. Don't 
call it "require".

> >> $.dimensions = {};
> >
> > OK, here we use $package('dimensions',{}) with jsPax - not really more
> > complicated.
>
> Yeah, but I don't see that we actually gain anything that way.

1. If you use Stuff like $.dimensions = {}; You pollute the jQuery internal 
namespace. Maybe use something like $.plugins.dimensions = {}.

2. You can not easily express subplugins. Maybe you would like to partition 
the functionality of a more complex plugin in multiple files and have a 
central js-file that needs all the others, but each one can used stand allone 
or requires just a subset of the other files. Load one of them and it maybe 
tries $.myplugin.partone = {}. That fails if $.myplugin does not exist, that 
is if the main file is not loaded. The main file is not necessary for the 
code that just needs explicitly that part of your plugin:

- myplugin.js
$.myplugin = {} // we can not wait for the file to be fully evaluated.
                // that is bad for multythreaded JS implementations as well as
                // for exceptions that arise while myplugin.js is evaluated.
$.ajax({ url: 'www.example.com/js/myplugin/part1.js',
         async: false,
         dataType: 'script' });
$.ajax({ url: 'www.example.com/js/myplugin/part2.js',
         async: false,
         dataType: 'script' });
$.ajax({ url: 'www.example.com/js/myplugin/part3.js',
         async: false,
         dataType: 'script' });
$.ajax({ url: 'www.example.com/js/myplugin/part4.js',
         async: false,
         dataType: 'script' });
-

- myplugin/part1.js
if(!$.myplugin) $.myplugin = {};
$.myplugin.part1 = {};
...
-
- myplugin/part2.js
if(!$.myplugin) $.myplugin = {};
$.myplugin.part2 = {};
...
-
- myplugin/part3.js
if(!$.myplugin) $.myplugin = {};
$.myplugin.part3 = {};
$.require('myplugin.part1,myplugin.part2');
...
-
- myplugin/part4.js
if(!$.myplugin) $.myplugin = {};
$.myplugin.part4 = {};
$.require('myplugin.part1,myplugin.part3');
...
-

Please check if myplugin.js has been loaded (not only one of the parts).


Just to add a potential solution for all of the problems:

jQuery.checkForPlugins = function(names) {
        var ns = (typeof names == 'string')?names.split(','):names, i, p, n, j;
        for( i = 0; i < ns.length; i++) { // this is still faster than $.each()
                                          // and not too much more code.
                                          // I don't have a real problem if 
                                          // you change that to use $.each().
                p = jQuery.plugins;
                n = (ns[i]+'.available').split('.');
                for( j = 0; p && j < n.length; j++) {
                        if( !p[n[j]] )
                                throw "required plugin " + ns[i] + " not found";
                        p = p[n[j]];
                }
        }
}
jQuery.plugin = function(name) {
        var p = jQuery.plugins;
        var n = name.split('.');
        for( var i = 0; i < n.length; i++ ) {
                if( !p[n[i]] ) p[n[i]] = {};
                p = p[n[i]];
        }
        p[n].available = true;
}

// Usage:
$.checkForPlugins('autocomplete,dimensions,myplugin.part1');
// or
$.checkForPlugins(['autocomplete','dimensions','myplugin.part1']);

// at the end of the plugin:
$.plugin('dimensions');

That is very similar to what jsPax does for dependency tracking. JsPax also 
has to maintain the state that a package has alreday been requested but has 
not finished loading by now, i.e. is not available.

Christof

Reply via email to