2015-10-06 61 views
0

Im正在努力解决使用Meteor JS的问题。流星js |通过帮手显示Json在视图中

我调用API至极返回我JSON数组至极的样子一来就这个URL返回(我不把整个数组这里大小的原因):https://blockchain.info/address/12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX?format=json&offset=0

我把它称为服务器端的像:

if (Meteor.isServer) { 
    Meteor.methods({ 
     getWalletPreviousTx: function() { 
     var url = "https://blockchain.info/address/12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX?format=json&offset=0"; 
     var result = Meteor.http.get(url); 
     if(result.statusCode==200) { 
      var tx = JSON.parse(result.content); 
      return tx; 
     } else { 
      console.log("Response issue: ", result.statusCode); 
      var errorJson = JSON.parse(result.content); 
      throwError("Couldn't fetch wallet balance from Blockchain, try again later !"); 
     } 
     } 
    }); 
} 

,我又把它通过一个帮手特定的模板取回我的看法:

Template.wallet.helpers({ 
    addrTxs: function() { 
     Meteor.call('getWalletPreviousTx', function(err, tx) { 
     console.log(tx); 
     return [tx]; 
     }); 
    } 
}); 

在帮手的console.log实际登录我的JSON数组WIC h表示它可以访问它。 现在部分IM挣扎是检索此JSON我的看法,我已经尝试了很多办法,没有他们的工作,其实我有这个在我看来:

<template name="wallet"> 
    <table> 
      {{#each addrTxs}} 
       <ul> 
        {{> addrTx}} 
       </ul> 
      {{/each }} 
    </table> 
</template> 

了JSON的一部分我想显示的是“地址”和每个交易的“价值”:

"inputs":[ 
    { 
    "sequence":4294967295, 
    "prev_out":{ 
     "spent":true, 
     "tx_index":97744124, 
     "type":0, 
     "addr":"1AWAsn8rhT555RmbMDXXqzrCscPJ5is5ja", 
     "value":50000, 
     "n":0, 
     "script":"76a914683d704735fd591ba9f9aebef27c6ef00cbd857188ac" 
    } 
    } 
] 

事实是,我从来没有设法在我看来,以显示从这个JSON数组任何东西,甚至puting直接这在我看来,没有按” t show anything:

{{addrTxs}} 

我在做什么错?有人能帮忙吗 ?

感谢您的阅读。

-----------------------编辑---------------------

我认为问题更多的是我的帮手和模板在api调用完成之前加载(因为在我的页面呈现之后console.log显示在我的控制台中,就像3秒钟一样)。我如何让我的帮手等到api调用完成之后才将它呈现在视图中?我使用铁路由器。

我试图以我的路线上添加waitOn动作要等到我的API调用完成:

Router.route('/wallet', { 
    name: 'wallet', 
    template: 'wallet', 
    loadingTemplate: 'loading', 
    waitOn: function() { 
    Meteor.call('getWalletPreviousTx', function(error, result) { 
    if(!error) { 
     Ready.set(result) 
    } 
    }); 
    return [ 
    function() { return Ready.get(); } 
    ]; 
}, 
action: function() { 
    if (this.ready()) 
     this.render(); 
    else 
     this.render('loading'); 
} 

}); 

与waitOn动作上面的代码似乎工作(我有没有错误),但我不知道在我看来,以显示来自特定结果的方式:

if(!error) { 
    Ready.set(result) 
} 

回答

1

交易都包含在tx.txs,通过迭代。

Template.wallet.helpers({ 
    addrTxs: function() { 
     Meteor.call('getWalletPreviousTx', function(err, tx) { 
     console.log(tx); 
     return tx.txs; 
     }); 
    } 
}); 

你说的没错,你需要使用异步调用使用会话变量。

首先,创建的调用方法:

Template.wallet.created = function() { 
    Meteor.call('getWalletPreviousTx', function(err, tx) { 
    console.log(tx.txs); 
    Session.set('tx', tx.txs); 
    }); 
}; 

助手应该是这样的:

Template.wallet.helpers({ 
    addrTxs: function() { 
    return Session.get('tx'); 
    } 
}); 
+0

感谢响应。我试着用你的方式,在我看来什么也没发生,但如果我console.log(tx.txs)它返回到我需要的数组数组。 我认为问题不在我的视图代码中,更重要的是当助手在视图中呈现时,api调用未完成。如何在加载模板之前等待通话结束? – Simonux

+0

您能否提供您使用{{> addrTx}}调用的模板? –

+0

这是我的模板 <模板名称= “钱包”>

{{#each addrTxs}} ​​{{> addrTx}} {{/每}}
但我真的认为这个问题来自于我的服务器端调用在我的模板加载之前没有完成。我在我的路线上用waitOn函数更新了我的问题。 – Simonux