AJAX - The Complete Reference

(avery) #1

PART II


Chapter 5: Developing an Ajax Library 181


Now, the first step in sendRequest() is to create a generic object that will be used to
wrap the XHR along with a number of useful values about the request.

sendRequest : function (url,options) {

var request = new Object();

Next is the private variable, _requestID, which is used to identify the request number.
This could be set to a more complicated facility with some unique identifier generation, but
for now it is kept simple, just incrementing the count with each access.

/* increment our requestID number */
request.requestID = ++AjaxTCR.comm._requestID;

Initially, the number of outstanding requests is managed here, though later it will be
moved to another function to support aborting and queuing properly.

/* increment requests outstanding */
AjaxTCR.comm._requestsOutstanding++;

Initialization also addresses setting a number of defaults that would be expected, such
as assuming the use of the GET method, favoring asynchronous requests, and using the
default content type x-www-form-urlencoded. Also note that some of the values, like the
requestContentTransfer encoding or payload, are set to a blank value. The assumption
here is that the user will set them if they need to when they invoke a request.

/* basic communication defaults */
request.method = "GET";
request.async = true;
request.preventCache = false;
request.requestContentType = AjaxTCR.comm.DEFAULT_CONTENT_TYPE;
request.requestContentTransferEncoding = "";
request.payload = "";

Standard callbacks are also defined for a successful response as well as a failed response.
Of course, the user can pass in what the callbacks ought to be, but they are set to a function
stub in case this is a one-way style of request or the user only wants to employ callbacks on
certain behaviors.

/* standard callbacks */
request.onSuccess = function(){};
request.onFail = function(){};

Finally, in the most basic cases, there are a number of flags to indicate the status of the
request as aborted or received.

/* communication status flags */
request.abort = false;
request.inProgress = false;
request.received = false;
Free download pdf