AJAX Events (^) ❘ 211
.show()
.prev()
.attr('src', arrow);
}
);
}
else
{
$(this).next().toggle();
if ($(this).attr('src').indexOf('down') != -1)
{
arrow = 'tree/right.png';
}
$(this).attr('src', arrow);
}
}
);
}
);
The preceding modifi cation gives you the same outcome demonstrated by Example 7-6, only this
time the functions that reveal and hide the activity indicator are attached by using jQuery’s AJAX
event methods instead of the $.ajaxSetup() method. This example takes things one step further
by taking a look at the options.url property, decoding URI encoded characters with a call to
decodeURI(), and then limiting the application of the activity indicator based on the URI of the
AJAX request. Things are set up similarly to Example 7-6; otherwise, you moved the callback
function for the beforeSend property to inside the call to the ajaxSend() method, and the callback
function for the success property to inside the call to the ajaxSuccess() method, and fi nally the
call for the error property to inside the call to the ajaxError() method. And those methods are, of
course, chainable like most of jQuery’s other methods, but these methods must be applied to the
document object and cannot be attached to just any HTML element object.
Attaching AJAX Events to Individual Requests
The last way that you can attach events is via a call to jQuery’s more low-level ajax() method. The
ajax() method is used internally, within jQuery, to construct AJAX requests for jQuery’s other
AJAX request methods, like $.get(), $getJSON(), $.post(), and so on. jQuery’s $.ajax() method
gives you the ability to set as many low-level AJAX request options as you like. Example 7-8 demon-
strates how to use $.ajax() to mimic the same results as the preceding two examples:$(document).ready(
function()
{
$('img.folderTreeHasChildren').click(
function()
{
var arrow = 'tree/down.png';