What it does is:
- Reads from a RSS feed using Google Feed API
- Shows the list in an unordered list
How good/bad is the code snippet?
$(document).ready(function(){
var FeedManager = {
config : {
feedContainer : $('#feedContainer'),
feedUrl : 'http://rss.bdnews24.com/rss/english/home/rss.xml',
feedLimit : 10
},
init: function(){
var feed = new google.feeds.Feed(FeedManager.config.feedUrl);
feed.setNumEntries(FeedManager.config.feedLimit) ;
feed.load(function(result) {
if (!result.error) {
FeedManager.$feedContainer = FeedManager.config.feedContainer;
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
$('<li/>').append(
$('<a>'+entry.title+'</a>').attr(
{
'title': entry.title,
'href': entry.link
}
).bind('click',FeedManager.showStory)
).appendTo(FeedManager.$feedContainer);
}
}
else{
FeedManager.handleError(result.error.message);
}
});
},
showStory: function(){
var href = event.currentTarget.href;
FeedManager.showURL(href);
event.preventDefault();
},
showURL: function(url){
if (url.indexOf("http:") != 0 && url.indexOf("https:") != 0) {
return;
}
chrome.tabs.create({
url: url
});
},
handleError: function(errorText){
$('<li/>')
.css("color","red")
.append("Error:"+errorText)
.appendTo(FeedManager.config.feedContainer);
}
};
FeedManager.init();
});
At the 2nd stage, I wanted to add custom accordion feature and news snippet:
processFeedResult: function(result) {
if (result.error) {
FeedManager.handleError(result.error.message);
return;
}
FeedManager.$feedContainer = FeedManager.config.feedContainer;
$.each(result.feed.entries, function() {
$('<li/>').append(
$('<a>', { // this should not be an anchor tag,right? TBD
title: 'Published at: '+this.publishedDate,
text: this.title
})
).append($('<div>', {
text: this.contentSnippet,
css : {'display':'none',
'padding-top':'2px'}
}).append(
$('<a>', {
text: '[more..]',
href: this.link,
click: FeedManager.showStory
}))
)
.bind('click',FeedManager.showSnippet)
.appendTo(FeedManager.$feedContainer);
});
},
showSnippet: function() {
var $obj = $(event.currentTarget),
$snippetDiv = $obj.find('div').slideDown('normal');
if(FeedManager.$lastOpenedDiv === undefined){
FeedManager.$lastOpenedDiv = $snippetDiv ;
}
else{
FeedManager.$lastOpenedDiv.slideUp('normal');
FeedManager.$lastOpenedDiv = $snippetDiv ;
}
}
};
I feel that I have put some tightly coupled code in my processFeedResult
such as text and CSS. I also wanted to know if my showSnippet
function good enough or not. However, it works, and I know that there are 3rd party good accordion available, but I wanted to learn it.
So far I've kept the anchor tag to show the news title and I used anchor title as a tooltip that shows the time. Maybe there is a good alternate, like span or paragraph?