2014-02-15 100 views
0

我使用流星, ,我有一個車把標記在我的HTML流星,存根方法和流星方法

{{displayResult}} 
在我的客戶端JS文件

我寫的助手和存根方法,這樣

輔助函數 * 編輯 *

displayResult:function(){ 
    var abc; 
    var apiResultDependency = new Deps.Dependency(); 
    Meteor.call('apiresult',function(e,result){ 
    abc=result; 
    apiResultDependency.changed();           
    });          
               console.log(abc);             
              console.log(result); 

apiResultDependency.depend(); 
    return abc;//returning nothing 
} 

存根方法

Meteor.startup(function(){    
    return Meteor.methods({ 
     apiresult:function(){ 
      console.log("loading..."); 
     } 
    }); 
}); 

,並用一個API和延遲結果連我的服務器代碼,我的代碼是

apiresult:function(){ 
    var response = returnAllResult();//this gets the result from outside func. working good 
    return response; 
} 

我想借此從服務器端功能的結果,我想顯示如何接收並顯示它的html文件 。我沒有在我的網頁上找到任何東西。在我的控制檯中打印結果。

回答

0

問題是,當數據從服務器到達時,模板不會重新渲染。要解決這個問題,最簡單的方法是使用反應性數據源模式(看here),所以在您的客戶端代碼,你將需要添加類似:

var apiResultDependency = new Deps.Dependency(); 
var abc; 

和你的助手可能是這樣的:

displayResult: function() { 
    if (abc === undefined) { 
    Meteor.call('apiresult', function(e,result) { 
     abc = result; // make sure this is not undefined to prevent infinite loop 
     apiResultDependency.changed(); 
    }); 
    } 
    apiResultDependency.depend(); 
    return abc; 
} 
+0

這是我調用服務器端方法時的主要問題,它首先將abc變量設置爲undefined。我現在正在循環運行。 – Sasikanth

+0

是的,我明白這一點。你有沒有檢查我的建議?我非常確定這應該可以正常工作。 –

+0

我寫了一個存根方法,它返回「loading ..」字符串,它不起作用,仍然返回未定義的value.how來完成它。 – Sasikanth