HTML and CSS Reference
In-Depth Information
var transport = tddjs.ajax.create();
options.transport = transport;
transport.open(options.method || "GET", options.url, true);
setHeaders(options);
transport.onreadystatechange = function () {
if (transport.readyState == 4) {
requestComplete(options);
transport.onreadystatechange = tddjs.noop;
}
};
transport.send(options.data);
}
ajax.request = request;
function get(url, options) {
options = tddjs.extend({}, options);
options.method = "GET";
ajax.request(url, options);
}
ajax.get = get;
function post(url, options) {
options = tddjs.extend({}, options);
options.method = "POST";
ajax.request(url, options);
}
ajax.post = post;
}());
The ajax namespace now contains enough functionality to serve most uses
of asynchronous communication, although it is far from complete. Reviewing the
implementation so far seems to suggest that refactoring to extract a request ob-
ject as the baseline interface would be a good idea. Peeking through the code in
Listing 12.66, we can spot several helpers that accept an options object. I'd sug-
gest that this object in fact represents the state of the request, and might as well have
been dubbed request at this point. In doing so, we could move the logic around,
making the helpers methods of the request object instead. Following this train of
thought possibly could lead our ajax.get and ajax.post implementations to
look something like Listing 12.67.
 
Search WWH ::




Custom Search