var callback = { success: handleResponse,
failure: handleFailure,
timeout: 5000, /* timeout in 5 seconds */
argument: {username: "thomas" , dog: "Angus" , example: true}
};
You should note that the values placed in the argument property are not sent during
any communication; the property is used solely as a convenient way to pass data around
without explicit creation of closures. Then in the various callback functions, the argument
can be accessed as part of the returned response object.
function handleResponse(response)
{
var name = response.argument.username;
var dog = response.argument.dog;
var example = response.argument.example;
/* do something interesting */
}
PART II
C h a p t e r 5 : D e v e l o p i n g a n A j a x L i b r a r y 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.
Pages:
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313