Online Book Reader

Home Category

AJAX In Action [261]

By Root 3909 0
to jonathan zheng

536

CHAPTER 13

Building stand-alone applications with Ajax

We also need to add it to our onReadyState function (listing 13.20). Listing 13.20 Code placement for onReadyState

net.ContentLoader.onReadyState=function(){

if(window.netscape &&

window.netscape.security

.PrivilegeManager.enablePrivilege)

netscape.security.PrivilegeManager

.enablePrivilege('

UniversalBrowserRead');

Both of these functions interact with the data from the outside world. That is why we are required to add this functionality in both locations. When the script is executed, we will get a message prompt informing us of the request to change the security settings (figure 13.14).

If we simply click the Allow button at the prompt, the security prompt will still open every single time the function is accessed. To avoid this, click the “Remember this decision” checkbox. That way, the browser makes a note of your decision and allows the XMLHttpRequest to execute every time it is accessed without issuing the prompt. With the security settings of the browser changed, we are able to make this application work off the desktop with Mozilla, Firefox, and Netscape. We can Figure 13.14

The security prompt notifies

the user about the request

for access rights.

Licensed to jonathan zheng

Refactoring

537

access XML feeds from any site without having to open multiple tabs or windows by using this reader. We also have the ability to alter this application to obtain other information from the Web, such as weather and comics.

13.6.2 Changing the application scope

This application is not limited to being an XML syndication reader from sites. We can easily adapt it as a banner ad rotator, company news updater, an event calendar, and so much more. For instance, we can store our banner ads within an XML document. That way, anyone can update the XML file with new ads without having to touch any of the HTML files or the server-side code. We can preload the banner ads and have them displayed in the reader. Instead of just having one ad on the screen, we can have them rotate through as the user is reading the site.

We can set up the XML document to hold the company news so we can display our current articles to the employees or customers. We just need to fill in the basic items of the XML feed. We can also make it display the updates to the site or any other information we want. As you can see, we are not limited to just the plain XML feeds.

13.7 Refactoring

Now that we have a fully developed script for reading RSS feeds, let’s take the time once again to improve upon our efforts. As mentioned earlier, there are lots of possibilities for extending our script in terms of perusing different types of content. In this section, we concentrate on reorganizing the script along ModelView-Controller (MVC) boundaries. As we explained in chapters 3 and 4, the MVC

pattern is a very common design pattern for separating the responsibilities of software. We’ll start our discussion with defining the Model types, then we’ll create a View for the Model, and finally we’ll round out the discussion with the Controller that ties everything together. 13.7.1 RSS reader Model

The RSS reader we’ve developed in this example will definitely benefit from having some first-class Model types to deal with. This will make the software conceptually cleaner and easier to read and maintain. With Ajax-based applications putting a heavier emphasis on the client DHTML than more traditional web applications, it becomes increasingly important to write clean, maintainable software. The Model classes we develop should also be generally applicable to other Licensed to jonathan zheng

538

CHAPTER 13

Building stand-alone applications with Ajax

applications that deal with RSS feeds. As a syntactic simplification, we’ll use the Prototype library to define types just as we did in chapter 10.

Let’s start by defining a Model class for an RSS feed. An RSS feed is for our purposes an XML document that adheres to a predefined

Return Main Page Previous Page Next Page

®Online Book Reader