multiple feed in one website using Google API for dynamic rss links

1,291 views
Skip to first unread message

simplybj

unread,
May 25, 2012, 12:17:13 AM5/25/12
to Google AJAX APIs

http://stackoverflow.com/questions/10736784/multiple-feed-in-one-website-using-google-api-for-dynamic-rss-links

I am using Google feed API to load rss in my website.
The Procedure mentioned in https://developers.google.com/feed/v1/devguide
Works Well for fetching rss from a link.
var feed = new google.feeds.Feed("http://stackoverflow.com/feeds");
//other Codes
google.setOnLoadCallback(myFunction);

My scenario is to load multiple rss feed in my website for Dynamic
Links
For example at an instance if links are as follows:

http://www.codeplex.com/site/feeds/rss
http://stackoverflow.com/feeds
http://fastpshb.appspot.com/feed/1/fastpshb/rss
and i tried to fetch Rss for multiple dynamic link.

$('li').each(function(index, value)
{
var text = $(this).text();
var feed = new google.feeds.Feed(text);
});
//Other Codes
google.setOnLoadCallback(myFunction);

Only the Last Link's rss is get fetched.

On this link https://developers.google.com/feed/v1/reference#resultFind
it is mentioned that
.setOnLoadCallback(callback) is a static function that registers the
specified handler function to be called once the page containing this
call loads, where callback is a required function called when the
containing document is loaded and the API is ready for use (e.g.,
after onLoad).

Is that the reason .setOnLoadCallback execute the last link?
What is the solution to this problem?

Jeremy Geerdes

unread,
May 25, 2012, 12:37:36 AM5/25/12
to google-ajax...@googlegroups.com
Without being able to see your complete code, it's tough to say what's going on here. However, I think that what is probably happening is that you are rapidly creating a large number of Feed objects and assigning them each, in turn, to feed. You are then loading only the last one (feed.load is not in your loop). To get a more exact answer, you'll need to provide a link to your page, working or not.

Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

For more information or a project quote:
jrge...@gmail.com

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!
> --
> You received this message because you are subscribed to the Google
> Groups "Google AJAX APIs" group.
> To post to this group, send email to
> google-ajax...@googlegroups.com
> To unsubscribe from this group, send email to
> google-ajax-searc...@googlegroups.com
> To view this message on the web, visit
> http://groups.google.com/group/google-ajax-search-api?hl=en_US
> For more options, visit this group at
> http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en

simplybj

unread,
May 25, 2012, 1:41:40 AM5/25/12
to Google AJAX APIs
Here is the code.
First I am using
<script type="text/javascript">
google.load("feeds", "1");
</script>

js to load feed is as:
(function($)
{
$.Load = function(settings)
{
Options = $.extend({ url: '', divID: '', noOfFeed: 4 },
settings);
var Rss = {
initialize: function()
{
var feed = new google.feeds.Feed(Options.url);
feed.setNumEntries(Options.noOfFeed);
var li = '';
var divOne = '';
var div = '';
feed.load(function(result)
{
if (!result.error) {
var feeddivid = $('#' + Options.divID);
li += '<li>' + result.feed.link + '</li>';
for (var i = 0; i <
result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
divOne += '<a href="' + entry.link + '"
target="_blank">' + entry.title + ' </a><br />';
}
}
$('#vtab ul').append(li);
div += '<div class="rssContent">' + divOne + '</
div>';
$('#vtab').append(div);

});
},
GoogleRss: function()
{
google.setOnLoadCallback(Rss.initialize);
},
init: function()
{
Rss.GoogleRss();
}
};
Rss.init();
};
$.fn.GoogleLoadRss = function(Settings)
{
$.Load(Settings)
}
} (jQuery));



I am calling this js from another js file Passing dynamic link:

$('#divRSSLinks li label').each(function(index, value)
{
var text = $(this).text();
$.Load({ url: text, divID: 'feed', noOfFeed: 4 });
});
All i get is the feed from last link repeted over number of links.
Did this help you to figure out the problem?
On May 25, 9:37 am, Jeremy Geerdes <jrgeer...@gmail.com> wrote:
> Without being able to see your complete code, it's tough to say what's going on here. However, I think that what is probably happening is that you are rapidly creating a large number of Feed objects and assigning them each, in turn, to feed. You are then loading only the last one (feed.load is not in your loop). To get a more exact answer, you'll need to provide a link to your page, working or not.
>
> Jeremy R. Geerdes
> Generally Cool Guy
> Des Moines, IA
>
> For more information or a project quote:
> jrgeer...@gmail.com
>
> If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!
>
> On May 24, 2012, at 11:17 PM, simplybj wrote:
>
>
>
>
>
>
>
>
>
> >http://stackoverflow.com/questions/10736784/multiple-feed-in-one-webs...
>
> > I am using Google feed API to load rss in my website.
> > The Procedure mentioned inhttps://developers.google.com/feed/v1/devguide
> > Works Well for fetching rss from a link.
> > var feed = new google.feeds.Feed("http://stackoverflow.com/feeds");
> > //other Codes
> > google.setOnLoadCallback(myFunction);
>
> > My scenario is to load multiple rss feed in my website for Dynamic
> > Links
> > For example at an instance if links are as follows:
>
> >http://www.codeplex.com/site/feeds/rss
> >http://stackoverflow.com/feeds
> >http://fastpshb.appspot.com/feed/1/fastpshb/rss
> > and i tried to fetch Rss for multiple dynamic link.
>
> > $('li').each(function(index, value)
> > {
> >    var text = $(this).text();
> >    var feed = new google.feeds.Feed(text);
> > });
> > //Other Codes
> > google.setOnLoadCallback(myFunction);
>
> > Only the Last Link's rss is get fetched.
>
> > On this linkhttps://developers.google.com/feed/v1/reference#resultFind

Jeremy Geerdes

unread,
May 25, 2012, 9:50:59 AM5/25/12
to google-ajax...@googlegroups.com
Rather than posting code into the group (which is notorious for messing up formatting and such), a link to your page would be best. Just looking at your code, I'm thinking that there is a lot more complexity here than there needs to be. Beyond that, I'm thinking that the code is not triggering as you think it should be. Namely, you're calling $.Load with the settings of each feed, in succession, and resetting the onload callback each time. It would be far better if you call google.setOnLoadCallback once, to a function that will get your li elements and their values and, for each one, call a common function with the appropriate settings. Something more like this:

(function(){
function init(){
$('#divRSSLinks li label').each(function(index, value){
loadFeed({
url : $(this).text(),
divId : 'feed',
noOfFeed : 4
});
});
}

function loadFeed(opt_options){
var feed = new google.feeds.Feed(opt_options.url);
feed.setNumEntries(opt_options.noOfFeed);
feed.load(function(result){
if(!result.error){
var feeddiv = $('#' + opt_options.divId),
li = '<li>' + result.feed.link + '</li>',
divOne = '';
$('#vtab ul').append(li);
for(var i=0; i<result.feed.entries.length; i++){
var entry = result.feed.entries[i];
divOne += '<a href="' + entry.link + '" target="_blank">' + entry.title + '</a><br />';
}
$('#vtab').append('<div class="rssContent">' + divOne + '</div>');
}
});
}

google.setOnLoadCallback(init);
})()


You'll have to be on the lookout for syntax errors from wrapped lines. And I haven't tried to run it at all to make sure that it works beyond that. But it should be close.

Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

For more information or a project quote:
jrge...@gmail.com

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!

simplybj

unread,
May 28, 2012, 2:08:09 AM5/28/12
to Google AJAX APIs
Thanks Jeremy.
Sorry for posting code here.
Thanks for the help its Working Now.
Bj
Reply all
Reply to author
Forward
0 new messages