Online Book Reader

Home Category

AJAX In Action [262]

By Root 4072 0
structure and has a URL

that specifies how it can be accessed. The primary attributes of the structure are the title, link, and description, with many other optional attributes, as discussed earlier. The feed also has several items, which can be thought of as the articles of its content. Let’s start by capturing what we know so far, as represented in listing 13.21.

Listing 13.21 The RSSFeed Model class

RSSFeed = Class.create();

RSSFeed.prototype = {

initialize: function( title, link, description ) {

this.title = title;

this.link = link;

this.description = description;

this.items = [];

},

addItem: function(anItem) {

this.items.push(anItem);

}

};

This code defines the RSSFeed type via the Prototype library Class.create(). You will recall that using this idiom, the initialize method is invoked by the generated constructor. So with this definition of our RSS feed Model class, a feed could be constructed via the following line of code:

var rssFeed = new RSSFeed( 'JavaRanch News',

'http://radio.javaranch.com/news/',

'Stories from around the ranch' );

This is pretty much all that’s required for the definition of an RSSFeed Model object. Notice that the RSSFeed has an addItem API that enables the addition of items to the internal item’s array. Each item should be a Model object as well—

one that encapsulates the attributes of each item in the feed. Given what we know about the RSS items, let’s define our item Model class as shown in listing 13.22.

Licensed to jonathan zheng

Refactoring

539

Listing 13.22 The RSSItem Model class

RSSItem = Class.create();

RSSItem.prototype = {

initialize: function( rssFeed, title, link, description ) {

this.rssFeed = rssFeed;

this.title = title;

this.link = link;

this.description = description;

}

};

Nothing much to get excited about here. The item encapsulates the title, link, and description attributes but also holds a reference to the RSSFeed object that it belongs to. Given these two Model classes, now we can envision that an item and one of its feeds could be constructed as shown here:

var rssFeed = new RSSFeed( 'JavaRanch News',

'http://radio.javaranch.com/news/',

'Stories from around the ranch' );

var feed1 = new RSSItem( rssFeed,

'Win a copy of JBoss',

'http://radio.javaranch.com/news/05/07/20/9.html',

'Text of Article' );

rssFeed.addItem(feed1);

So far, so good. The Model is a very straightforward encapsulation of the attributes of an RSS feed and its items. The two Model classes that encapsulate these two concepts are RSSFeed and RSSItem, respectively. Now let’s consider the construction of the Model itself. We know that these objects will get instantiated as a result of the XML data being loaded into the client by an Ajax request. So let’s define an API that our Ajax handler can call for converting the XML response into an instance of our RSSFeed Model class. Let’s start by defining the contract of our Model creator as follows:

var rssFeed = RSSFeed.parseXML( rssXML );

This contract implies that we’ll pass the XML response returned from our Ajax handler to the parse method of our RSSFeed type, and it will return to us an instance of an RSSFeed. Given that assumption, let’s implement the parseXML() method as shown in listing 13.23.

Licensed to jonathan zheng

540

CHAPTER 13

Building stand-alone applications with Ajax

Listing 13.23 The RSS XML parsing

RSSFeed.parseXML = function(xmlDoc) {

var rssFeed = new RSSFeed(

RSSFeed.getFirstValue(xmlDoc, 'title'),

RSSFeed.getFirstValue(xmlDoc, 'link' ),

RSSFeed.getFirstValue(xmlDoc, 'description'));

var feedItems = xmlDoc.getElementsByTagName('item');

for ( var i = 0 ; i < feedItems.length ; i++ ) {

rssFeed.addItem(new RSSItem(rssFeed,

RSSFeed.getFirstValue(feedItems[i], 'title'),

RSSFeed.getFirstValue(feedItems[i], 'link' ),

RSSFeed.getFirstValue(feedItems[i], 'description'))

}

return rssFeed;

}

This method does the textbook response XML parsing that we’ve done many times already. It takes the values of the title, link, and description elements

Return Main Page Previous Page Next Page

®Online Book Reader