繁体   English   中英

流星,存根和流星方法

[英]Meteor, stub method and meteor methods

我正在使用流星,并且我的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文件中显示如何接收和显示它。 我的网页上没有任何内容。 在我的控制台中,它正在打印结果。

问题是,当数据从服务器到达时,模板不会重新呈现。 解决此问题的最简单方法是使用反应性数据源模式(请参见此处 ),因此在客户端代码中,您需要添加以下内容:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM