HTML and CSS Reference
In-Depth Information
13.1.2.1 Defining the Object
The first thing we expect from the interface is simply that it exists, as Listing 13.3
shows.
Listing 13.3 Expecting tddjs.ajax.poller to be an object
(function () {
var ajax = tddjs.ajax;
TestCase("PollerTest", {
"test should be object": function () {
assertObject(ajax.poller);
}
});
}());
This test jumps the gun on a few details; we know that we are going to want to
shorten the full namespace, and doing so requires the anonymous closure to avoid
leaking the shortcut into the global namespace. Implementation is a simple matter
of defining an object, as seen in Listing 13.4.
Listing 13.4 Defining tddjs.ajax.poller
(function () {
var ajax = tddjs.namespace("ajax");
ajax.poller = {};
}());
The same initial setup (anonymous closure, local alias for namespace) is done
here as well. Our first test passes.
13.1.2.2 Start Polling
The bulk of the poller's work is already covered by the request object, so it is simply
going to organize issuing requests periodically. The only extra option the poller
needs is the interval length in milliseconds.
To start polling, the object should offer a start method. In order to make any
requests at all we will need a URL to poll, so the test in Listing 13.5 specifies that
the method should throw an exception if no url property is set.
 
Search WWH ::




Custom Search