Engine Methods and Properties


When we create the callback method for the request, we will need to check the ready state of the response in order to know that it is completed and ready to be parsed. In order to check the ready state with our engine, we will call our first Ajax method named checkReadyState. Following is a snippet of our onResponse method, which will make this call:

function onResponse() {     if(Ajax.checkReadyState('loading') == "OK")     {         // Parse response here     } }


After the ready state of the response is complete, we can parse the data as we did in Chapter 3. In order to parse the data, the Ajax object has another method named getresponse. This method will provide the correct data format based on the header response from the server. In other words, you do not have to worry about itit all happens for you, which is a reason why this object makes life easier. Therefore, if there is a response and its content type is XML, this method will return the responseXML with the first documentElement so that the data is immediately ready to be parsed. If the data is not XML, the method will return the reponseText value, which can be used in any way you see fit. Following is a quick example of how simple it is to use this method in our onResponse callback:

[View full width]

function onResponse() { if(Ajax.checkReadyState('loading') == "OK") { var categories = Ajax.getResponse().getElementsByTagName ('category'); for(var i=0; i<categories.length; i++) { document.getElementById("body").innerHTML += Ajax.getResponse(). getElementsByTagName('category')[i].firstChild.data +"</br>"; } } }


This sample code will get all the category nodes from the XML and iterate through them in order to populate the body element's innerHTML property, resulting in a list of categories on the page. Since JavaScript does not have any data typing or member visibility, it is obviously not a requirement to use this method to receive a response because it is only a way to make things easier during development. If you simply want to view the response as a string, you can call the request property in the Ajax object directly and use it to access the responseText and/or responseXML property.

This chapter is an example of how the engine simplifies our requests by eliminating a lot of the overhead and allowing us to focus on other aspects of the application. The next two chapters will explain how easy it is to debug with the engine in place and how flexible the engine is by allowing us endless ways of extending and scaling it.



Ajax for Web Application Developers
Ajax for Web Application Developers
ISBN: 0672329123
EAN: 2147483647
Year: 2007
Pages: 129
Authors: Kris Hadlock

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net