AJAX - The Complete Reference

(avery) #1

PART II


Chapter 5: Developing an Ajax Library 179


favorite language appealing. However, the readability and support problems that may
result over time from this could be significant.
Fortunately, a handful of Ajax-focused libraries are well implemented, widely supported,
and moving toward addressing all the possible cases that may interest you. We’ll look at
those briefly toward the end of the chapter. For now, let’s get started building a library to
assist with Ajax development. When you graduate to another environment, you will be
armed with the experience and understanding of features and concerns, so your selection
will be more informed than if you simply selected whatever offering was currently in vogue.

NNOT EOTE If you are aware of Yahoo’s YUI library, you will find the AjaxTCR library approach to be
quite familiar; if you aren’t, you will find that you can migrate quickly to this widely supported
library when you are ready to move on from learning. We present some YUI basics toward the
end of the chapter.

AjaxTCR Library Overview


The wrapper library developed for the book primarily focuses on the communication
aspects of Ajax since this is the foundation of an Ajax application and we all know what
happens to any project with a weak foundation. This library in its complete form can be found
at http://ajaxref.com/lib/ajaxtcr.js and an overview of its syntax at http://ajaxref.com/
lib/docs, as well as in Appendix C. However, before you start reading the code, you might
want to read along here first. The library will be presented here incrementally, starting with
its overall architecture. Many of the features in the final form need a bit of explanation.
First, following the good practices of many of the libraries we admire, such as YUI
(http://developer.yahoo.com/yui/), the library is wrapped in an object literal to form a
basic namespace.

var AjaxTCR = { };

This means that various functions and variables will be prefixed by this value. Encapsulation
continues under this primary namespace with a subobject comm, which will hold all the
communications properties and methods.

AjaxTCR.comm = { };

This breakdown is added as there is a need for many supporting features in the examples
that aren’t directly communication related. To start exploring the Ajax-specific features of
the library, let’s look at a number of useful constant values defined such as alphanumeric
identifiers for the various readyState values shown here:
/* readyState constants as defined by w3c */

UNSENT : 0,
OPEN : 1,
SENT : 2,
LOADING : 3,
DONE : 4,
Free download pdf