Online Book Reader

Home Category

AJAX In Action [254]

By Root 4031 0
all of the URLs from which we want to obtain information. For each iteration, we increment our loader array to hold the new ContentLoader request. We pass in the URL of the feed, the function BuildXMLResults() that formats the content, and the function BuildError() that will be called if there is an error obtaining the feed. Now that we have begun the loading process, we need to format the returned XML feeds.

Adapting the function

When the request is made, it is going to call either BuildXMLResults() (if it was successful) or BuildError() (if it encountered any problems). BuildXMLResults() takes the XML feed and formats it into a usable format. BuildError() logs the error to the error list. Both functions update the status so we can see the progress of the loading. Listing 13.8 shows the implementation of this logic. Listing 13.8 Formatting the XML results into a JavaScript array

function BuildXMLResults(){

var xmlDoc = this.req.responseXML.documentElement;

var RSSTitle =

xmlDoc.getElementsByTagName('title')[0].firstChild.nodeValue;

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

for(iC=0;iCintMessage = arrayMessage.length;

arrayMessage[intMessage] = new Array(

RSSTitle,

xRows[iC].getElementsByTagName('title')[0]

.firstChild.nodeValue,

xRows[iC].getElementsByTagName('link')[0]

Licensed to jonathan zheng

522

CHAPTER 13

Building stand-alone applications with Ajax

.firstChild.nodeValue,

xRows[iC].getElementsByTagName('description')[0]

.firstChild.nodeValue);

}

UpdateStatus();

}

The function BuildXMLResults() in listing 13.8 retrieves the XML document by referencing our request object’s responseXML property. With the XML document stored in our local variable xmlDoc, we are able to obtain the RSS title information for the feed. To do this, we reference the title element tag and reference the first child node’s value.

We obtain the item elements and prepare to loop through the resulting array stored in xRows. By looping through the array, we are able to create a multidimensional array, storing it in the next position of our global array, arrayMessage. The global array holds the title of the RSS feed and the title, link, and description of the article. We build this multidimensional array for every item element stored in xRows. After we’ve finished traversing the document, we call the function UpdateStatus() (listing 13.9) to display the current state of the process to the user. Listing 13.9 Function informing user of preloading functionality

function UpdateStatus(){

intLoadFile++;

if(intLoadFile < arrayRSSFeeds.length){

document.getElementById("divNews2").innerHTML =

"Loaded File " + intLoadFile + " of "

+ arrayRSSFeeds.length + strErrors;

}else if(intLoadFile >= arrayRSSFeeds.length && !bLoadedOnce){

document.getElementById("divNews2").innerHTML =

"Loading Completed" + strErrors;

if(arrayMessage.length == 0){

alert("No RSS information was collected.");

return false;

}

bLoadedOnce = true;

var timerX = setTimeout("ChangeView()",1500);

}

}

The function UpdateStatus() performs two services, as shown in listing 13.9. The first service displays the status of the preloader to the user. The second service determines if the slideshow has to be started. We first increment our global Licensed to jonathan zheng

Loading the RSS feeds

523

variable intLoadFile to update the file count. If intLoadFile is less than the total files we are to load, we display our loading status by setting the innerHTML of our top layer divNews2 with our output string.

If the file count is greater than or equal to the number of files in our array (and also the slideshow has not been started), then we can start the transitions. Before we can start the slideshow, we need to verify that we actually have data to show. We verify the data by checking the length of our formatted message array, arrayMessage. If there are no messages, we notify the user and exit the function by returning false.

If there is data to display, we set bLoadedOnce to true and call the

Return Main Page Previous Page Next Page

®Online Book Reader