JavaScript回调函数和Google Feed API [英] JavaScript Callback Functions and Google Feed API

查看:197
本文介绍了JavaScript回调函数和Google Feed API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用div填充函数在我的网站中实现几个Feed。以前,我有相同的JavaScript代码块通过我的页面(每个饲料一个),但为了成为一个好的(或更好的)编码器,我决定把重复的代码粘贴到一个外部脚本文件,并运行它与不同的参数



不幸的是,我的新实现不能加载Feed,我的想法已经不多了。我根据用户ndp的有趣的建议结构我的回调函数问题9662168



所以,我在外部脚本文件中有以下两个函数简单):

  function populateRSSFeed(divID){
var targetDiv = document.getElementById(divID);
return function callback(result){
if(!result.error){
var container = document.getElementById(targetDiv);
var list = document.createElement('ul');
< snip:div-population code here>
container.appendChild(list);
}
else
alert('Error fetching feeds!');
}
}


函数initializeRSSFeed(callback,targetFeed){
google.load('feeds','1');
var feed = new google.feeds.Feed(targetFeed);
var numEntries = 3;
feed.setNumEntries(numEntries);
feed.load(callback);
}

这些函数在我的HTML中调用如下:

 < script> 
var callback = populateRSSFeed('feed-list-wrapper-1');
initializeRSSFeed(callback,'http://rss.cnn.com/rss/cnn_topstories.rss');
< / script>

我收到的错误消息让我认为在进料加载阶段,但我无法确定原因。

 未捕获的TypeError:无法读取未定义的属性'Feed'



您认为什么?

解决方案

您似乎未使用 setOnLoadCallback 等待Google指令码载入。这将解释为什么 google.feeds 未定义(直接导致您看到的错误)。



使用此脚本(我只是遵循开发人员指南中的Hello World示例) )。

  google.load('feeds','1'); 
function onGoogleReady(){
var callback = populateRSSFeed('feed-list-wrapper-1');
initializeRSSFeed(callback,'http://rss.cnn.com/rss/cnn_topstories.rss');
}
google.setOnLoadCallback(onGoogleReady);


I'm trying to implement a few feeds into my site using a div-populating function. Previously, I had identical JavaScript code blocks through my page (one for each feed), but in the interest of being a good (or better) coder, I decided to stick the repetitive code into an external script file and run it with varying parameters instead.

Unfortunately, the feeds refuse to load with my new implementation, and I'm running out of ideas. I structured my callback function based on user ndp's interesting suggestions in question 9662168.

So, I have the following two functions in my external script file (minor code redaction for simplicity):

function populateRSSFeed(divID) {
    var targetDiv = document.getElementById(divID);
    return function callback(result) {
        if (!result.error) {
            var container = document.getElementById(targetDiv);
            var list = document.createElement('ul');
            <snip: div-population code here>
            container.appendChild(list);
        }
        else
            alert('Error fetching feeds!');
    }
}


function initializeRSSFeed(callback, targetFeed) {
    google.load('feeds','1');
    var feed = new google.feeds.Feed(targetFeed);
    var numEntries = 3;
    feed.setNumEntries(numEntries);
    feed.load(callback);
}

These functions are called as follows in my HTML:

<script>
    var callback = populateRSSFeed('feed-list-wrapper-1');
    initializeRSSFeed(callback, 'http://rss.cnn.com/rss/cnn_topstories.rss');
</script>

The error message I'm receiving makes me think that something is going wrong at the feed-loading stage, but I can't determine the cause.

Uncaught TypeError: Cannot read property 'Feed' of undefined

What do you guys think?

解决方案

It looks like you're not using setOnLoadCallback to wait for the Google scripts to load. That would explain why google.feeds is undefined (resulting directly in the error you're seeing).

Try using this script instead (I just followed the "Hello World" example in the Developers' Guide).

google.load('feeds','1');
function onGoogleReady() {
    var callback = populateRSSFeed('feed-list-wrapper-1');
    initializeRSSFeed(callback, 'http://rss.cnn.com/rss/cnn_topstories.rss');
}
google.setOnLoadCallback(onGoogleReady);

这篇关于JavaScript回调函数和Google Feed API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆