views:

157

answers:

2

I would like to use the Google AJAX Feed API to fetch multiple newsfeeds and display them on a webpage.

I can't use Google's FeedControl class because I want to control the way the items of the feed are displayed. That leaves me with Google's Feed class as the only option.

I've got it to work with a single feed: provide a url and a callback function; process the results of the feed inside the callback function.

My question: How do i fetch multiple feeds? I would somehow have to make a new google.feeds.feed object inside the callback function and provide it with a new url and ... the same callback-function (?)

I never studied computer sciences, so this kind of recursion makes my head spin. Anyone can explain what I have to do?

A: 

Yes, you can do that okay, just make sure that whenever you do recursion you have two cases (a) the base case and (b) the recursive case.

Only in the second case does the function call itself, make sure you don't in the base case or else you will end up with an infinite recursion. For example, in this factorial function 0 is the base case ,everything else is the recursive case

function fact(n) {
    if (n==0) { return 1; }
    else { return n * factorial(n-1) }
}

Alternatively, you could do them one at a time, ie. start the first one, then start the second one, instead of having to wait for one to completely load before the second starts loading.

Jeffrey Aylesworth
Sorry, I just had to point out the classic error in your factorial function. The base case, n==0 should return 1, not zero.
Steve Folly
Thanks for pointing that out, I really need to test my code before I post it
Jeffrey Aylesworth
+2  A: 

Sure, you can do it that way, here's some pseudocode:

// 'feeds' is an array of the feed URLs
function grabFeeds(feeds) {
    var index;

    // We start with the first feed
    index = 0;

    // Kick off the process
    feedWorker();

    // Our "go get the next feed" function
    function feedWorker() {
        var feed;

        // Do we have any more?
        if (index < feeds.length) {
            // Yes, request it and bump our index
            // (You could combine these lines, but it's
            // clearer to keep them separate)
            feed = feeds[index];
            ++index;
            start_feed_download(feed, callback);
        }
    }

    // Our callback function
    function callback() {
        // ...do something with the result...

        // Kick of the next feed (feedWorker is defensive,
        // so we don't have to check index here)
        feedWorker();
    }
}

I don't know the Google Feed API, hence the placeholder start_feed_download function.

What that does is start the process of grabbing the feeds via the grabFeeds function, which accepts an array of feeds. grabFeeds kicks off feedWorker, which initiates the first feed request, and then returns immediately (almost certainly before the first feed is retrieved). callback processes the result, then asks feedWorker to kick off the next feed request (if there is one).

The "magic" here is that feedWorker and callback are both closures, so even though grabFeeds has already returned, the index and feeds variables live on (in an object called an "execution context") for as long as anything references the things inside the execution context -- in our case, until callback and feedWorker are no longer referenced by Google's stuff.

T.J. Crowder
thanks, that did the trick. Thanks for the added explanation about closures. Reading up on it now.
Jan