2013-05-07 61 views
1

我有一个未执行的回调函数。我怀疑它被视为一个字符串,但我不确定。 代码如下。 此外,这里有一个简化的jsFiddle更详细:http://jsfiddle.net/oakley808/sH5XE/3/Javascript回调函数未执行

基本上它只是迭代for循环,使用对象的设置。最后一行config.feeds[i].cb是失败的。想法任何人?

// the callback function 
function rssdone(){ 
    $('#cbBlock').append('did some callback stuff<br>');  
} 

// the settings for the loop below 
var config = { 
    "feeds": [ 
    { 
     "container": "#block1", 
     "url":"http://apps1.eere.energy.gov/news/rss/program.cfm?topic=1010", 
     "limit":"4", 
     "layoutTemplate": "<ol type='1'>{entries}</ol>", 
     "entryTemplate": "<li>{title}</li>", 
     "cb":"rssdone" 
    }, 
    { 
     "container": "#block2", 
     "url":"http://apps1.eere.energy.gov/news/rss/financial_opps_solar.cfm", 
     "limit":"2", 
     "layoutTemplate": "<ol type='A'>{entries}</ol>", 
     "entryTemplate": "<li>{title}</li>", 
     "cb":"rssdone" 
    } 
    ] 
}  

// the logic  
for(var i=0; i < config.feeds.length; i+=1) { 
    $(config.feeds[i].container).rss(
    config.feeds[i].url, 
    { 
     limit:   config.feeds[i].limit, 
     layoutTemplate: config.feeds[i].layoutTemplate, 
     entryTemplate: config.feeds[i].entryTemplate 
    }, 
    // this fails to use the callback for some reason 
    config.feeds[i].cb 

    // use this instead and it works! 
    // rssdone 
); 

} 

回答

2

是的,你存储你的配置对象为json而不是js对象。引号使其被视为字符串。来自各地的“rssdone”引用删除引号,它应该工作:

var config = { 
    "feeds": [ 
    { 
     "container": "#block1", 
     "url":"http://apps1.eere.energy.gov/news/rss/program.cfm?topic=1010", 
     "limit":"4", 
     "layoutTemplate": "<ol type='1'>{entries}</ol>", 
     "entryTemplate": "<li>{title}</li>", 
     "cb":rssdone 
    }, 
    { 
     "container": "#block2", 
     "url":"http://apps1.eere.energy.gov/news/rss/financial_opps_solar.cfm", 
     "limit":"2", 
     "layoutTemplate": "<ol type='A'>{entries}</ol>", 
     "entryTemplate": "<li>{title}</li>", 
     "cb":rssdone 
    } 
    ] 
}  
1

jsFiddle Demo

那是因为它只是一个字符串。你可以做什么只是让这些持有你的RSS功能的对象,然后使用字符串访问调用函数

var cbHolder = {}; 
cbHolder.rssdone = function(){ 
$('#cbBlock').append('did some callback  stuff<br>'); 
}; 

然后

cbHolder[config.feeds[i].cb] 
0

你在这是正确的,你是唯一当这个.rss插件期待一个函数时传递一个字符串。你不能激发一个字符串。

如果您想要引用一个函数作为字符串引用,那么您将需要引用该函数作为某个作用域对象的方法。

例如:

var obj = { 
    something: function() { 
    alert('here') 
    } 
} 

function executeCallback (reference, scope) { 
    scope[reference]() 
} 

executeCallback('something', obj) 

在这里看到: http://jsfiddle.net/sH5XE/4/

0

解决方案1 ​​

该解决方案是证明3仅在配置对象可以是一个真正的JS对象。

Fiddle with the solution 1

解决方案2

如果你要找回你的配置对象throught AJAX,这将是一个JSON对象,是不是可以从它引用的回调函数。在这种情况下的一种可能的形式给出是存储全局范围(窗口对象)上的回调函数,并使用该变量名称来引用功能:

// the callback function 
window.rssdone = function(){ 
    $('#cbBlock').append('did some callback stuff<br>');  
}; 

// the settings for the loop below 
var config = { 
    "feeds": [ 
    { 
     ... 
     "cb":"rssdone" 
    }, 
    { 
     ... 
     "cb":"rssdone" 
    } 
    ] 
}  ; 

// the logic  
for(var i=0; i < config.feeds.length; i+=1) { 
    $(config.feeds[i].container).rss(
    config.feeds[i].url, 
    { 
     limit:   config.feeds[i].limit, 
     layoutTemplate: config.feeds[i].layoutTemplate, 
     entryTemplate: config.feeds[i].entryTemplate 
    }, 
    // reference to the callback on the global scope 
    window[ config.feeds[i].cb ]; 
); 
} 

Fiddle with the solition 2