如何获取Meteor.Call返回模板的值? [英] How to get Meteor.Call to return value for template?

查看:477
本文介绍了如何获取Meteor.Call返回模板的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过了解这个帖子有关这个概念,但是,我没有得到它。我有以下简单的设置:

I've tried to understand this post regarding this concept, however, I'm failing to get it. I have the following simple setup:

/server/test.js
Meteor.methods({ 
  abc: function() {
    var result = {};
    result.foo = "Hello ";
    result.bar = "World!";
    return result;
  }
});

/client/myapp.js
var q = Meteor.call('abc');
console.log(q);

此结构返回到控制台 undefined

This structure returns to the console undefined.

如果我将 myapp.js 文件更改为:

Meteor.call('abc', function(err, data) {
  !err ? console.log(data) : console.log(err);
}

我收到 Object 我的控制台。

理想情况下,这是我想要能够做的,但它不工作,说在控制台:无法读取未定义的属性问候

Ideally this is what I'd like to be able to do, but it doesn't work, stating in the console: Cannot read property 'greeting' of undefined

/client/myapp.js
var q = Meteor.call('abc');

Template.hello.greeting = function() {
   return q.foo;
}

任何将数据从服务器对象传递到模板中的帮助都将非常感谢,我还在学习JavaScript& Meteor。

Any help in passing the data from the server object into the template would be greatly appreciated. I'm still learning JavaScript & Meteor.

谢谢!

推荐答案

//docs.meteor.com/#meteor_call\"> Meteor.call 文档:

From the Meteor.call documentation:


在客户端上,如果你没有传递一个回调,并且你不在一个存根,调用将返回未定义,你将没有办法获得方法的返回值。这是因为客户端没有光纤,所以实际上没有任何方法可以阻止远程执行一个方法。

On the client, if you do not pass a callback and you are not inside a stub, call will return undefined, and you will have no way to get the return value of the method. That is because the client doesn't have fibers, so there is not actually any way it can block on the remote execution of a method.

所以,你会想这样做:

Meteor.call('abc', function(err, data) {
  if (err)
    console.log(err);

  Session.set('q', data);
});

Template.hello.greeting = function() {
  return Session.get('q').foo;
};

这将在数据可用后反应更新模板。

This will reactively update the template once the data is available.

这篇关于如何获取Meteor.Call返回模板的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆