HTML and CSS Reference
In-Depth Information
Listing 12.67 Possible direction of the request API
(function () {
/* ... */
function setRequestOptions(request, options) {
options = tddjs.extend({}, options);
request.success = options.success;
request.failure = options.failure;
request.headers(options.headers
||
{});
request.data(options.data);
}
function get(url, options) {
var request = ajax.request.create(ajax.create());
setRequestOptions(request, options);
request.method("GET");
request.send(url);
};
ajax.get = get;
function post(url, options) {
var request = ajax.request.create(ajax.create());
setRequestOptions(request, options);
request.method("POST");
request.send(url);
};
ajax.post = post;
}());
Here the request.create takes a transport as its only argument, meaning
that we provide it with its main dependency rather than having it retrieve the object
itself. Furthermore, the method now returns a request object that can be sent when
configured. This brings the base API closer to the XMLHttpRequest object it's
wrapping, but still contains logic to set default headers, pre-process data, even out
browser inconsistencies, and so on. Such an object could easily be extended in order
to create more specific requesters as well, such as a JSONRequest . That object
could pre-process the response as well, by for instance passing readily parsed JSON
to callbacks.
 
Search WWH ::




Custom Search