MooTools, Framework Compatibility, and Dollar $afe Mode

MooTools 1.2.3 was released on June 19, 2009. With this release, MooTools implemented Dollar Safe Mode. Essentially, MooTools no longer requires the $ function for its own purposes. Valerio Proietti writes about Dollar Safe Mode on the MooTools blog but I'll give a quick overview below.

document.id() vs $

The $ function has become document.id(). The code looks as follows:

/* old:: var myElement = $('my-element'); */ var myElement = document.id('my-element');

If MooTools doesn't detect another framework requesting the $ function, $ will automatically be assigned to MooTools.

document.search() vs $$

The $$ function will also be renamed document.search(). The code looks as follows:

/* old:: var inputs = $$('input'); */ var input = document.search('input');

Like the $ function, $$ will be assigned to MooTools' document.search() if a $$ function is not detected. Note that document.search() was NOT implemented in MooTools 1.2.3; document.search() will be implemented in MooTools 2.

Strategies For Updating < 1.2.3 MooTools Code

There are a couple of ways to update your code. The first is to simply switch all $ references to document.id():

/* old:: var myElement = $('my-element'); */ var myElement = document.id('my-element');

The second method is to use closures so you can continue to use the $ and $$ functions:

(function($,$$) { this.MyClass = new Class({ //functionality in here... }); var myInstance = new MyClass($('search-box'),$$('input')) })(document.id,document.search);

DO NOT FEAR! My MooTools Code Still Works!

Don't pass up my older code samples due to the move away from requiring $! The code will still work. Use one of the above strategies for making my code framework-compatible.