Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

The Aggregator Object

The Aggregator object is the one that receives the response from the AJAX engine. The following code shows the creation of the object (an array called feedCollection, which will be used to store all of the feed objects that will be created from the feeds that are retrieved), and a method called Read (the callback method for the request from the index.html form). When the callback occurs, the readyState of the request is checked through a custom AJAX object method, which takes a string parameter representing a DIV element that will display a loading message.

Aggregator = new Object();
Aggregator.feedCollection = new Array();
Aggregator.Read = function()
{
  if(Ajax.checkReadyState(’loading’) == "OK")
  {
    var title = Ajax.getResponse().getElementsByTagName(’title’)[0].firstChild.data;
    var _link = Ajax.getResponse().getElementsByTagName(’link’)[0].firstChild.data;
    var items = Ajax.getResponse().getElementsByTagName(’item’);

    var feed = new Feed(Aggregator.feedCollection.length, title, _link, items);
    Aggregator.feedCollection.push(feed);
    Aggregator.displayFeedTitles(feed);
  }
}

The first thing that we do in the Read method is parse the title, link, and items from the RSS feed. Once we have these values, we create a new Feed object, which we’ll focus on later. This object takes the length of the feedCollection as an ID, and the title, link, and items from the feed. The Feed object is then added to the feedCollection and a method called displayFeedTitles is called to display the titles for each of the items in the Feed object.

Aggregator.displayFeedTitles = function(feed)
{
  document.getElementById(’titles’).innerHTML += feed.GetTitle();
  Aggregator.DisplayTitles(feed.id);
}

This method takes the Feed object as a parameter, displays its title, and then calls another method called DisplayTitles:

Aggregator.DisplayTitles = function(id)
{
  var titleArray = Aggregator.feedCollection[id].GetAllTitles();
  var titles = document.createElement("div");
  titles.id = "subTitle_"+ id;
  document.getElementById(’title_’+id).appendChild(titles);
  for(var i=0; i<titleArray.length; i++)
  {
    titles.innerHTML += titleArray[i] +"<br />";
  }
}

This method receives a feed ID and uses it to retrieve the feed from the feedCollection array and get all of its titles. Once the titles are received, we’ll create a new DIV element for the item titles in the feed and append it after the title for that particular feed. This will allow us to toggle the item titles in the display by clicking the feed title. (We’ll add this code in a bit.) Once the new DIV element is appended, we simply iterate all of the titles and add them to the new DIV.

The first of the last two methods is for toggling the titles from the items in the feed, and the second is for displaying the contents of a feed in our description DIV element in the index.html file. The contents of the feed are gathered by calling the GetDetails method of the Feed object, which we’ll cover in the next section when we create the Feed object.

Aggregator.ToggleTitles = function(id)
{
  var titles = document.getElementById(’subTitle_’+id);
  titles.style.display = (titles.style.display == ’’) ? ’none’ : ’’;
}


Aggregator.DisplayFeed = function(feedId, id)
{
  var details = Aggregator.feedCollection[feedId].GetDetails(id);
  document.getElementById(’description’).innerHTML = details;
}
  • + Share This
  • 🔖 Save To Your Account