我使用的是流星,我的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
}Stub方法
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文件中显示如何接收和显示它。我的网页上什么都没有。在我的控制台中,它正在打印结果。
发布于 2014-02-15 12:20:22
问题是,当数据从服务器到达时,模板不会重发。解决这一问题的最简单方法是使用反应性数据源模式(如这里),因此在客户端代码中需要添加如下内容:
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;
}https://stackoverflow.com/questions/21797121
复制相似问题