【问题标题】:Wait with rendering Meteor template helper until variable is available等待渲染 Meteor 模板助手,直到变量可用
【发布时间】:2017-05-30 07:26:54
【问题描述】:

我正在 Meteor 中创建一个基于位置的聊天应用程序。现在我只想渲染用户区域中的聊天消息。 TheRegion.region 变量被 HTML5 地理定位请求填充。

Template.locationchat.helpers({
  messages: function () {
    return Messages.find({location: TheRegion.region});
  }
});

这段代码的问题是,当调用这个助手时,TheRegion.region 变量仍然是null。有没有办法在地理定位函数的回调中运行帮助程序?或者当变量有值时运行模板助手?

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    我经常在 Meteor 中发现,如果你在等待一个变量,你只需要一个 if 子句来保护自己。

    试试这个:

      Template.locationchat.helpers({
        messages: function () {
            if(TheRegion.region)
                return Messages.find({location: TheRegion.region});
        }
      });
    

    感觉不自然,但通常可以。试试看吧。

    【讨论】:

      【解决方案2】:

      那是因为你的变量不是反应式的。

      在您的 onCreated 中:

      TheRegion = new ReactiveDict();
      TheRegion.set('region',undefined);
      

      现在,区域在到达助手时总是存在的,当值更改时,您的助手将重新运行。

      【讨论】:

      • 谢谢..我首先安装了 reactivedict 包,然后它立即工作!
      猜你喜欢
      • 2014-01-16
      • 2015-05-13
      • 1970-01-01
      • 2016-04-30
      • 2014-07-23
      • 2017-11-16
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      相关资源
      最近更新 更多