keif's posterous

Quick Dojo Setup Snippet for MooTools Developers

We're all used to aliasing methods within our favorite JavaScript frameworks. For example, you'll see the following pattern within jQuery:

(function($) {
        //your jQuery here, referenced by $
})(jQuery);
Copy this code to the clipboard
1(function($) {
2 //your jQuery here, referenced by $
3})(jQuery);

You may also see the following pattern within MooTools code:

(function($) {
        //your MooTools here, referenced by $
})(document.id);
Copy this code to the clipboard
1(function($) {
2 //your MooTools here, referenced by $
3})(document.id);

Within the Dojo community I frequently see the following pattern:

;(function(d,$) {
        //your dojo here, selector engine referenced by $
})(dojo,dojo.query);
Copy this code to the clipboard
1;(function(d,$) {
2 //your dojo here, selector engine referenced by $
3})(dojo,dojo.query);

I love that pattern but I've created my own that I think will be a bit easier for MooTools developers looking to use Dojo:

;(function(d,$,$$) {
        //your dojo here
        //byId referenced by $
        //selector engine referenced by $$
})(dojo,dojo.byId,dojo.query);
Copy this code to the clipboard
1;(function(d,$,$$) {
2 //your dojo here
3 //byId referenced by $
4 //selector engine referenced by $$
5})(dojo,dojo.byId,dojo.query);

While the $$ method is unique to MooTools, Dojo features both a byId method to get a single node and a query method to retrieve multiple nodes. You could just as easily retrieve one element with dojo.query but I like the security (and speed) that associating $ with byId gives me.

JavaScript shortcuts. the ';' is a safety in case there is no semi-colon on the previous line. Interesting.

To Posterous, Love Metalab