【问题标题】:Client Side Asynchronous Call in Meteor?Meteor中的客户端异步调用?
【发布时间】:2013-06-26 19:13:26
【问题描述】:

我有一个 Meteor 客户端异步调用,我似乎无法从中获取返回值。我知道我不能在客户端使用期货,所以我被卡住了。

由于 Meteor.call() 来自客户端,它必须是异步的。它看起来像这样:

Meteor.call('DirList', path, function(error, result) { console.log(result); });

console.log() 工作正常,但如何将结果返回到周围的函数中?

鲍勃

【问题讨论】:

    标签: asynchronous meteor client-side


    【解决方案1】:

    您可以将结果存储在Session 变量中,然后使用Deps.autorun 上下文中的变量执行逻辑。喜欢:

    Meteor.call('DirList', path, function(error, result) { Session.set('result', result); });
    Deps.autorun(function (c) {
      var result = Session.get('result');
      if (!result) return;
      c.stop();
      alert(result);
    });
    

    【讨论】:

    • @gabrielpuglises 是的,确实,这确实有效。谢谢!以这种方式使用会话变量(用于保存状态)感觉有点奇怪。你会说这是 Meteor 处理客户端异步返回变量的“最佳实践”还是“官方”方式?
    • 是的,这是执行此操作的常用方法,如果您继续使用 Meteor,您会发现它比您想象的更自然。请注意不要过度使用它。如果你这样做,你的代码将变得不可读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 2012-10-03
    • 2016-07-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2017-03-10
    相关资源
    最近更新 更多