Datasheet

The first instruction is to test whether there is a response. Obviously, BuzzWatch deserves better error
handling when this isn’t the case. The next instruction is to set a timer with the value gathered in the
Cache-Control: max-age HTTP header that has been set by the PHP script. This is done by a simple
BuzzWatch function,
getMaxAge()in script.js, that parses this header using JavaScript regular
expressions:
function getMaxAge(oResponse) {
var cacheControl = oResponse.getResponseHeader[‘Cache-Control’];
if (!cacheControl)
return undefined;
var result;
if (result=cacheControl.match(/^.*max-age=(\d+)(;.*)?$/))
return result[1];
return undefined;
}
There is an unfortunate tendency among developers to reinvent the wheel. Applied to web development,
this tendency often leads people to reinvent HTTP features. Many developers would have included an
XML attribute in the XML document to define when the document should be refreshed. The cache con-
trol header would still have been needed because it is used by cache managers in the browser and in
caching proxies that might sit between the server and the browser. Defining the same information twice
(once in the cache control header and once in the XML document) is always error prone: chances are
that when you’ll update the value in one of the locations, you forget to update it in the other one, and
it’s preferable to avoid this duplication when possible!
Back to the
handleListSuccess method. The next three instructions are for initializing the JKL.ParseXML
library with the XML that you’ve received from the server. This library is similar to SimpleXML in PHP and
it will release you from the burden of using a low-level API such as the DOM to parse the XML document.
After the last of these lines,
var tree = xotree.parseDOM( o.responseXML.documentElement );,
you have a JavaScript object in your variable
tree that has the same structure than the XML document.
The
handleListSuccess method is used each time the list of watches is reloaded from the server and it
needs to remove the previous content of the Go menu before inserting the content just received from the
server. This is done by a loop that removes and destroys the menu items.
The next step is to feed the menu with data read in the XML response. The structure from this document
is a
watches root element with a number of watch sub-elements, and the script loops over these watch
sub-elements. The JKL.ParseXML library, like most similar libraries, automatically creates an array when
it finds repeated elements and it has been told by the instruction
xotree.force_array = [“watch”]
to create such an array even if there is only a single watch element. The loop creates a menu item for
each
watch element. The menu items that were created so far were created from existing (X)HTML ele-
ments, but the menu items that are created in this loop are created entirely in JavaScript. Each menu
item is added to the menu and an event subscription is added to call the method
loadSymbol of the cur-
rent object (which is the controller) with the
watch symbol as a parameter when the user clicks on this
menu item.
At this stage, you have reached the point where the application is waiting for users to click one of these
menu items, unless they decide to create a new watch. This is also the end of the exchanges between the
browser and the server that correspond to the server log snippet shown earlier. If you click one of these
items, the following exchanges are added to the server’s log:
16
Chapter 1
04_087889 ch01.qxp 5/2/07 12:56 PM Page 16