在d3.json中返回匿名回调的结果

时间:2014-05-28 22:56:57

标签: javascript json d3.js

我正在调用一个从我的d3 javascript返回json的REST。我需要'rate'的值,这是由匿名函数产生的,返回到调用getJSON的任何东西。我该如何做到这一点?匿名函数是一个回调函数,在REST调用完成后执行。这是我的代码不起作用:

function getJson() {
d3.json('http://something.org/api/latest.json, function(data){
var rate = [{key:1, values:[{"label":"label","value":20},{"label":"label2","value":2}]}];
    for(var key in data.rates){
        if(data.rates.hasOwnProperty(key)){
            //need to return this variable after for loop to whoever's calling getJson
            rate[0]["values"].push({"label":key,"value":data.rates[key]});
        }
    }
    //rate is fully populated here

});
//can't access populated rate anymore
return rate;

1 个答案:

答案 0 :(得分:0)

Lars是对的。基本上,数据需要在匿名函数中处理。图也是如此。它们需要使用数据(或回调提供的响应变量)从匿名函数中编写。之后,调用渲染器。 详情请见http://digitallibraryworld.com/?p=487