AJAX - The Complete Reference

(avery) #1

PART II


Chapter 5: Developing an Ajax Library 205


Similar to argument, scope may be set. This should be set to an object that contains the
scope that the various callback handlers should run within.

var callback = { success: AjaxTCRExamples.handleResponse,
failure: AjaxTCRExamples.handleFailure,
timeout: 5000, /* timeout in 5 seconds */
argument: {username: "thomas" , dog: "Angus" , example: true}
scope: AjaxTCR.Examples
};

This kind of approach avoids the definition of a variable that to preserve the value of
this, which is a common technique in Ajax applications.

YUI Response Syntax
Once the connection object has been created, it will eventually invoke either the function
associated with the success or failure property in the callback object. The object returned for
a success callback function will have the standard properties and methods you would
expect for XHR including status, statusText, responseText, and responseXML. It will
also have the expected getResponseHeader() and getAllResponseHeaders() methods.
It pretty much seems to be like a standard XHR; however, it will also contain the tId
property, which contains the transaction ID of the particular connection object invoking the
callback. The object will also include the argument property containing the value(s) set
when creating the callback object. Because of the argument property, developers may not
need to understand JavaScript closures and scoping rules as deeply when using YUI.
In the case that the callback invoked is a failure, all the previously mentioned properties
will not necessarily be available. In the case that a communication failure which provides
little insight into the problem has occurred, a status value of 0 with a statusText of
“communication failure” will be set. In the case that the request is aborted using the
abort() method discussed later, status will have a value of –1 and the statusText will
read “transaction aborted.” In other cases, there may be values in status (for example, 500)
and statusText (for example, “Internal Server Error”) and even some headers or even
payload in responseText to look at. In any case, the tId and argument values will always
be available in the failure callback.

YUI Hello Ajax World
Given this brief syntax introduction, the “Hello World” example from Chapter 1 can be
rewritten directly using YUI:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Hello Ajax World - YUI Style</title>
<script src="http://yui.yahooapis.com/2.3.0/build/yahoo/yahoo-min.js"
type="text/javascript"></script>
<script src="http://yui.yahooapis.com/2.3.0/build/event/event-min.js"
type="text/javascript"></script>
<script src="http://yui.yahooapis.com/2.3.0/build/connection/connection-min.js"
type="text/javascript" type="text/javascript"></script>
<script type="text/javascript">
Free download pdf