moo.ajax

December 20 2005, 08:06:00

Notice: moo.ajax is now deprecated, as it has been replaced by mootools. Go get it now.

I noticed many of you requested if it would have been possible to use the full prototype, instead of the lite one I provided with the full download of moofx, to use its ajax capabilities along with the effects. Sure you can, but wouldn’t it be cooler if there was a more lightweigt one?

Just to continue my mission in small javascripting, I have arranged this moo.ajax, in just 1.3kb of uncompressed sweetness.

read more for instructions, or download the script. As always, any comments/corrections/suggestions are highly appreciated.

moo.ajax is a very simple ajax class, to be used with prototype.lite from moo.fx. It’s roughly based on the prototype one, so their usage are very similar.

The request

to make any request just call

new ajax(url, options);

The options

An example

This will call my script.php via post, setting as parameter sleep=3. When the request is complete I want an alert with the response text, and I want an element to be updated with the responsetext as well.

new ajax ('sleep.php', {postBody: 'sleep=3', update: $('myelementid'), onComplete: myFunction});
function myFunction(request){
  alert(request.responseText);
}

Is pretty simple, isn’t it? You can also watch a demo of this.

And now, the conclusion

This little script is meant to be used for simple ajax calls (take mooflex for example). If you need to use JSON or you intend to use ajax more extensively, I suggest you to use the full prototype instead.

Next week I’ll publish moo.dom, a lightweight (ofcourse!) script also based on prototype.lite, to provide easy dom navigation.