您现在的位置: 365建站网 > 365文章 > RSS 阅读器

RSS 阅读器

文章来源:365jz.com     点击数:200    更新时间:2009-09-14 10:29   参与评论

/*
    RSS 阅读器
*/

/* DisplayModule interface. */

var DisplayModule = new Interface('DisplayModule', ['append', 'remove', 'clear']);

/* ListDisplay class. */

var ListDisplay = function(id, parent) { // implements DisplayModule
    this.list = document.createElement('ul');
    this.list.id = id;
    parent.appendChild(this.list);
};

ListDisplay.prototype = {
    append: function() {
        var newEl = document.createElement('li');
        this.list.appendChild(newEl);
        newEl.innerHTML = text;
        return newEl;
    },
    remove: function(el) {
        this.list.removeChild(el);
    },
    clear: function() {
        this.list.innerHTML = '';
    }
};

/* Configuration object. */
var conf = {
    id: 'cnn-top-stories',
    feedUrlL 'http://rss.cnn.com/rss/cnn_topstories.rss',
    updateInterval: 60, //In seconds.
    parent: $('feed-readers')
};

/* FeedReader class. */
var FeedReader = function(display, xhrHandler, conf){
    this.display = display;
    this.xhrHandler = xhrHandler;
    this.conf = conf;
   
    this.startUpdates();
};

FeedReader.prototype = {
    fetchFeed: function() {
        var that = this;
        var callback = {
            success: function(text, xml) { that,parseFeed(text, xml); },
            failure: function(status) { that.showError(status); }
        };
        this.xhrHandler.request('GET','feedProxy.php?feed='+this.conf.feedUrl, callback);
    },
   
    parseFeed: function(responseText, responseXML) {
        this.display.clear();
        var items = responseXML.getElementsByTagName('item');
        for(var i = 0, len = items.length; i < len; i++) {
            var title = items[i].getElementsByTagName('title')[0];
            var link = items[i].getElementsByTagName('link')[0];
            this.display.append('<a href="'+link.firstChild.data + '">' +title.firstChild.data+'</a>');
        }
    },
    showError: function() {
        this.display.clear();
        this.display.append('Error fetching feed.');
    },
    stopUpdates: function() {
        clearInterval(this.interval);
    },
    startUpdates: function() {
        this.fetchFeed();
        var that = this;
        this.interval = setInterval(function() { that.fetchFeed(); }, this.conf.updateInterval * 1000);
    }
};

/* FeedManager namespace. */

var FeedManeger = {
    createFeedReader: function(conf) {
        var displayModule = new ListDisplay(conf.id + '-display', conf.parent);
        Interface.ensureImplements(displayModule, DisplayModule);
       
        var xhrHandler = XhrManager.createXhrHandler();
        Interface.ensureImplements(xhrHandler, AjaxHandler);
       
        return new FeedReader(displayModule, xhrHandler, conf);
    }

};

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (200人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 建站公司
· 杰创官网
· 建站工具

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2022 版权所有 鄂ICP备17013400号