Online Book Reader

Home Category

AJAX In Action [92]

By Root 4127 0
a script that has already been loaded into the browser. No recourse to the web server (the old presentation tier) is necessary, which is good for responsiveness and for server load. Such an interaction is suitable for relatively trivial calculations, such as adding a sales tax or shipping charge to a customer’s order. In general, for this approach to be effective, the client-side logic that processes the interaction needs to be small and unchanging during the lifetime of the customer interaction. In the case of shipping options, we are on safe ground because the number of options will be of the order of two to five, not several thousands (unlike, say, the full catalog of an online retailer), and the shipping costs are unlikely to change from one minute to the next (unlike, say, a stock ticker or first-come-first-served ticket-reservation system). This type of interaction has already been explored in chapter 4’s discussion of the client-side Controller, so we’ll say no more about it here. The remaining three categories all involve a trip back to the server and differ primarily in what is fetched. The key differences are summarized in the following sections, along with the pros and cons of each.

5.4.2 Introducing the planet browser example

Before we dive in to the different data exchange mechanisms, let’s introduce a simple example, to serve as a hook on which to hang our arguments. The application will present a range of facts about the planets of our solar system. Our main screen shows an idealized view of the solar system, with an icon for each planet. On the server, we have recorded various facts about these planets, which can be brought up in pop-up windows by clicking on the planet’s icon (figure 5.6). We aren’t using the ObjectViewer from chapter 4 here, but we will get back to it later in this chapter.

The part of the puzzle that interests us now is delivering the data shown in the pop-up from the server to the browser. We’ll look at the format of data that the server sends us in each variation, but we won’t go into the details of generating that data, as we’ve already covered the principles in our discussion of MVC in chapter 3. Listing 5.1 shows the skeleton of our client-side application, around which we can explore the various content-delivery mechanisms.

Licensed to jonathan zheng

176

CHAPTER 5

The role of the server

Figure 5.6

Screenshot of planetary info application, in which pop-up windows

describing each planet can be brought up by clicking on the icons.

Listing 5.1 popups.html

"-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Planet Browser

href="main.css"/>

href="windows.css"/>

href="planets.css"/>

libraries

Licensed to jonathan zheng

The details: exchanging data

177

d

Add hard-coded icons of planets

mercury

venus

earth

mars

®Online Book Reader