HTML and CSS Reference
In-Depth Information
this.xhr.complete(200, "OK");
Furthermore, if we expect the server to return JSON data, it would probably
be a good idea to indicate as much by sending the right Accept header with the
request. Separating Concerns
The current implementation has a code smell—something that doesn't feel quite
right. JSON parsing doesn't really belong inside a Comet client; its responsibili-
ties are delegating server-side events to client-side observers and publishing client-
side events to the server. Ideally the transport would handle correct encoding
of data. As I've mentioned more than a few times already, the ajax.request
should be refactored such that it provides an object that can be extended. This
would have allowed us to extend it to provide a custom request object specifi-
cally for JSON requests, seeing as that is quite a common case. Using such an
API, the connect method could look something like Listing 13.65, which is a lot
Listing 13.65 Using tailored JSON requests
function connect() {
if (!this.url) {
throw new TypeError("Provide client URL");
if (!this.poller) {
this.poller = ajax.json.poll(this.url, {
success: function (jsonData) {
Granted, such a poller could be provided with the current implementation of
ajax.request and ajax.poll , but parsing JSON belongs in ajax.poll as
little as it does in ajax.cometClient .
Search WWH ::

Custom Search