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 [1] 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.
#message { display:none; width:150px; text-align:center; padding:5px 8px; font-weight:bold; position:absolute; right:2%; font:12px arial; }
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>
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'});
}
}
Click here [2] to view an example.
Do you have any suggestions for my class? Please share!
Links:
[1] http://css.dzone.com/news/creating-gmail-ajax-status-dis
[2] http://davidwalsh.name/dw-content/ajax-message.php
[3] http://davidwalsh.name/dwrequest-mootools-12-ajax-listener-message