2015-10-06 62 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