Online Book Reader

Home Category

AJAX In Action [119]

By Root 4097 0
to be cleared and replaced by a low-level notification if everything went okay or by a medium-level notification if there was an error.

To implement this, we simply need to create Message objects at appropriate points in the lifecycle of the request, such as when the request is fired, when it completes or errors, and so on. The modified code for our ContentLoader object is given in listing 6.9.

Listing 6.9 ContentLoader with notifications

net.ContentLoader=function( ... ){ ... };

net.ContentLoader.msgId=1;

net.ContentLoader.prototype={

loadXMLDoc:function(url,method,params,contentType){

if (!method){

method="GET";

}

if (!contentType && method=="POST"){

contentType='application/x-www-form-urlencoded';

}

if (window.XMLHttpRequest){

this.req=new XMLHttpRequest();

} else if (window.ActiveXObject){

this.req=new ActiveXObject("Microsoft.XMLHTTP");

}

if (this.req){

try{

var loader=this;

this.req.onreadystatechange=function(){

loader.onReadyState.call(loader);

}

this.req.open(method,url,true);

if (contentType){

this.req.setRequestHeader('Content-Type', contentType);

Licensed to jonathan zheng

238

CHAPTER 6

The user experience

}

this.notification=new msg.Message( b

Notify request has started

"net00"+net.ContentLoader.msgId,

"loading "+url,

msg.PRIORITY_LOW.id,

-1,

"img/ball-earth.gif"

);

net.ContentLoader.msgId++;

this.req.send(params);

}catch (err){

this.onerror.call(this);

}

}

},

onReadyState:function(){

var req=this.req;

var ready=req.readyState;

if (ready==net.READY_STATE_COMPLETE){

var httpStatus=req.status;

if (httpStatus==200 || httpStatus==0){

this.onload.call(this);

this.notification.clear(); c

Clear initial notification

}else{

this.onerror.call(this);

}

}

},

defaultError:function(){ d

Notify on error

var msgTxt="error fetching data!"

+"

  • readyState:"+this.req.readyState

    +"

  • status: "+this.req.status

    +"

  • headers: "+this.req.getAllResponseHeaders()

    +"

";

if (this.notification){

this.notification.clear();

}

this.notification=new msg.Message(

"net_err00"+net.ContentLoader.msgId,

msgTxt,msg.PRIORITY_DEFAULT.id

);

net.ContentLoader.msgId++;

}

};

Licensed to jonathan zheng

Using the framework with network requests

239

When we make a network request using loadXMLDoc(), we create a low-level notification b and attach a reference to it to the ContentLoader object. Note that we set the lifetime to -1, so that the notification won’t expire by itself. In the onReadyState() method, we clear the notification if everything has gone well c. In the case of an error, we call the defaultError() method, which now generates a notification of its own d. The message for this notification uses HTML markup to create a richer report than plain text could.

Listing 6.10 demonstrates an example page using this modified ContentLoader. Listing 6.10 Notifications sample page

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Notifications test

Licensed to jonathan zheng

240

CHAPTER 6

The user experience

here is some content. This is

®Online Book Reader