AJAX - The Complete Reference

(avery) #1

196 Part II: Applied Ajax^


to select such nodes, but JavaScript does not directly support such syntax. We introduce a
facility called AjaxTCR.util.DOM.getElementsBySelector(selector, startNode)
that can take such a string found in the selector and find the result. A shorthand form of this
valuable function is $selector(). A few short examples of these helpful selection methods
are presented here:

/* get all nodes with the classname "red" */
var nodes = AjaxTCR.util.DOM.getElementsBySelector(".red");

/* get all div nodes with the class "red" and "large" */
var nodes = AjaxTCR.util.DOM.getElementsBySelector("div.red.large");

/* get all em tags that are direct children of a div tag with the id "id9" */
var nodes = AjaxTCR.util.DOM.getElementsBySelector("div#id9>em");

/* get all nodes that are children of a div tag with the class "blue" that are
the last child of their parent node */
var nodes = AjaxTCR.util.DOM.getElementsBySelector("div .blue:last-child");

/* get all input nodes that are of class form and are disabled */
var nodes = AjaxTCR.util.DOM.getElementsBySelector("input.form:disabled");

/* get all div nodes that have the id attribute contain the string "star" */
var nodes = AjaxTCR.util.DOM.getElementsBySelector("div[id*='star']");

To see some more examples, visit the example at http://ajaxref.com/ch5/utiltest.html.

AjaxTCR Library Basic Features Summary


At this point the library is fairly powerful, but it lacks a certain number of features to make
it more robust. Table 5-2 shows the two primary communication methods with which you
have interacted. The full syntax for the library can be found in Appendix C.
Calls to the sendRequest() method can get a bit complicated, as there are numerous
options that can be set. Table 5-3 summarizes what you have seen so far for this object.
We also introduced a number of useful helper methods to deal with request and
response data formats. The currently discussed methods of the data object are summarized
in Table 5-4.

NNOT EOTE While YAML was discussed in Chapter 4, it is not included in the library because although
the format is popular amongst Ruby developers it is not commonly used outside of that realm to
be of interest.

We also presented a few very basic methods for selecting DOM nodes more easily.
While certainly not as extensive as some of the libraries that you will encounter later in the
chapter, the methods shown in Table 5-5 cover the most common methods needed to
accomplish ease in DOM related coding.
The library developed to this point is quite capable. In fact, you can explore the
reimplementation of most of the previous examples from Chapter 3 and Chapter 4 at
http://ajaxref.com/ch5/refactored.html.
Free download pdf