Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor.Handlebar block助手的呼叫_Meteor - Fatal编程技术网

Meteor.Handlebar block助手的呼叫

Meteor.Handlebar block助手的呼叫,meteor,Meteor,我正在尝试在把手块帮助器中使用Meteor.call函数 Handlebars.registerHelper('get_handle', function(profileId, name) { Meteor.call("getProfileLink", profileId, function(error, result) { if (error) { return new Handlebars.SafeString('<a href="#">' + name

我正在尝试在把手块帮助器中使用
Meteor.call
函数

Handlebars.registerHelper('get_handle', function(profileId, name) {
  Meteor.call("getProfileLink", profileId, function(error, result) {
    if (error) {
      return new Handlebars.SafeString('<a href="#">' + name + '</a>');
    } else {
      return new Handlebars.SafeString('<a href="http://twitter.com/' + result + '">' + name + '</a>');
    }
  });
});
handlebar.registerHelper('get_handle',函数(profileId,name){
调用(“getProfileLink”、profileId、函数(错误、结果){
如果(错误){
返回新把手。安全字符串(“”);
}否则{
返回新把手。安全字符串(“”);
}
});
});

我在
console.log(result)
中看到返回了结果,但是没有呈现来自这个助手的HTML。但是,当我将相同的
handlebar.SafeString
返回值从
Meteor.call
中取出时,效果很好。我做错了什么?或者在Handlebar块中使用Meteor.call不正确吗?

在Handlebar块中不能使用Meteor.call,这主要是因为javascript的异步设计,当从服务器接收到值时,返回值已经返回

但是,您可以使用
会话
变量传递它:

Handlebars.registerHelper('get_handle', profileId, name,  function() {
    return new Handlebars.SafeString(Session.get("get_handle" + profileId + "_" + name));

});


//In a meteor.startup or a template.render
Meteor.call("getProfileLink", profileId, name, function(error, result) {
    if (error) {
       Session.set("get_handle" + profileId + "_" + name, '<a href="#">' + name + '</a>');
    } else {
       Session.set("get_handle" + profileId + "_" + name, '<a href="http://twitter.com/' + result + '">' + name + '</a>');
    }
});

谢谢你,阿克沙。我想我更愿意将其作为字段添加到文档中并检索,而不是稍后进行渲染。我对其进行了修改并为您添加了一种方法。我认为最简单的方法是在创建文档时存储句柄,并在检索列表时进行渲染。我以前没有存储句柄字段。不过还是谢谢你。我喜欢黑客:)
Handlebars.registerHelper('get_handle', profileId, name,  function() {
    if(Session.get("get_handle" + profileId + "_" + name)) {
        return new Handlebars.SafeString(Session.get("get_handle" + profileId + "_" + name));
    }
    else
    {
        Meteor.call("getProfileLink", profileId, name, function(error, result) {
            if (error) {
                Session.set("get_handle" + profileId + "_" + name, '<a href="#">' + name + '</a>');
            } else {
                Session.set("get_handle" + profileId + "_" + name, '<a href="http://twitter.com/' + result + '">' + name + '</a>');
            }
        });
        return "Loading..."
     }
});