David Walsh Blog

DWRequest: MooTools 1.2 AJAX Listener & Message Display

Though MooTools 1.2 is in its second beta stage, its basic syntax and theory changes have been hashed out. The JavaScript library continues to improve and become more flexible.

Fellow DZone Zone Leader Boyan Kostadinov wrote a very useful article detailing how you can add a Gmail-like AJAX display to your website using the Prototype JavaScript library. The article got me to thinking: could it be helpful to always display an AJAX notification message upon every request’s initial request, success event, and failure event? Maybe even only for debugging purposes?

Whatever your reason may be, I’ve created the DWRequest MooTools class to inform the user or developer of each AJAX request and subsequent result.

The CSS Code

#message	{ display:none; width:150px; text-align:center; padding:5px 8px; font-weight:bold; position:absolute; right:2%; font:12px arial; }

The XHTML Code

The following code is an example of two XHTML links that make AJAX calls. Note the message DIV.

<body>

	<div id="message">Performing Request...</div>
	<a href="inputs.php" class="ajax">Succeed</a><br />
	<a href="sug.php" class="ajax">Fail</a>

</body>

The MooTools JavaScript Code

var DWRequest = new Class({
	Extends: Request,
	options: {
		onRequest: function() {
			show_ajax_message('request');
		},
		onSuccess: function() {
			show_ajax_message('success');
		},
		onFailure: function() {
			show_ajax_message('failure');
		},
		onCancel: function() {
			show_ajax_message('cancel');
		}
	}
});

//adds initial events
window.addEvent('domready',function() {

	//for every action class
	$$('.ajax').each(function(el) {

		//add an event..
		el.addEvent('click', function(e) {
			e = new Event(e).stop();

			//make ajax request
			var req = new DWRequest({
				url: el.get('href'),
				method: 'get',
				data: 'something'
			}).send();
		});
	});
});

//shows the block
function show_ajax_message(state)
{
	//set position
	$('message').setStyle('top',window.getScrollTop() + 10);

	//on request...
	if(state == 'request')
	{
		//show the box
		$('message').addClass('onrequest').setText('Performing Request...').setStyles({'background-color':'#fffea1','display':'block','opacity':'100'});
	}
	//on success
	else if(state == 'success')
	{
		//take care of box
		$('message').set('text','Request Complete!');

		//do effect
		var myMorph = new Fx.Morph('message',{'duration':1000});
		myMorph.start({'opacity': 0,'background-color': '#90ee90'});
	}
	else if(state == 'failure')
	{
		//take care of box
		$('message').set('text','Request Failed!');

		//do effect
		var myMorph = new Fx.Morph('message',{'duration':1000});
		myMorph.start({'opacity': 0,'background-color': '#ff0000'});
	}
	else if(state == 'cancel')
	{
		//take care of box
		$('message').set('text','Request Cancelled!');

		//do effect
		var myMorph = new Fx.Morph('message',{'duration':1000});
		myMorph.start({'opacity': 0,'background-color': '#fffea1'});
	}
}

The Explanation

  1. Once the DOM is ready, any link with the class ajax is listened to — upon click, a DWRequest class instance is created.
  2. I’ve extended the MooTools Request class, adding default actions for the onRequest, onFailure, onCancel, and onComplete events. (Calls the show_ajax_message() function.)
  3. The function displays and manipulates the message DIV as needed: color morphing, message changes, and positioning.

Do you have any suggestions for my class? Please share!