Many of the JavaScript
libraries mentioned in the previous chapter include an Ajax component. This is a perfect
example of why JavaScript libraries are so popular: most of the hard work is already done for
you. With a larger user base, bugs are found more quickly, and many of the planning issues
are already thought out for you. Let??™s step through a few examples using various libraries.
Prototype
The Prototype library has some very handy Ajax functionality built in:
new Ajax.Request(url, {
method: 'get',
onSuccess: function(transport) { }
});
The format that the Prototype library takes is actually quite similar to the way you
approached the object. They go much farther in automating a number of features, however.
For example, there are event handlers for more than just success or failure. You can hook into
a number of events, such as the following:
??? onCreate: Is used after the object is instantiated but before any of the methods of the
object are used.
??? onComplete: Fires upon completion of the request and after the other event handlers
have been fired. This is a good place to stop any animation or loading indicator you
might be using.
??? onException: Fires if it could not process the request. For example, if there was an
improperly formatted JSON object returned, this event would fire.
??? onFailure: Fires if the call ends and there is no valid HTTP status code between 200
and 300 (similar to the way the custom object you saw earlier worked).
Pages:
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193