2014-02-15 62 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