【问题标题】:Meteor variable in template instead of function模板中的流星变量而不是函数
【发布时间】:2014-08-04 19:42:08
【问题描述】:

Meteor 空格键有没有办法存储模板函数的返回值? 我会解释得更好。

例如,假设我需要知道某个事件是否已启动。 在 client.js 上,我将拥有:

Template.event.isEventStarted = function(eventId) {
    var event = Events.findOne({_id: eventId});
    return Events.isStarted(event);
}

假设现在我需要从“事件”模板多次访问“isEventStarted”。我还需要从子模板访问它。显然,每次调用“isEventStarted”时都会执行查询,函数“isStarted”也是如此。他们执行客户端,但我可能有许多带有许多子模板的事件模板。

【问题讨论】:

    标签: function templates variables meteor spacebars


    【解决方案1】:

    这基本上就是 UI.emboxValue 的设计目的。尝试这样做:

    Template.event.isEventStarted = function (eventId) {
      return UI.namedEmboxValue(eventId, function () {
         /* ... */
      }, EJSON.equals);
    };
    

    这样可以确保只有在相应数据发生变化时才会重新计算查询。

    【讨论】:

    • 似乎我无法访问 emboxValue 函数中的集合。所以我试图将我的收藏文件移动到“lib”文件夹下,我可以在函数中使用“事件”收藏。但是“eventId”一直是“未定义的”。我使用 {{#if isEventStarted _id}} 从模板传递事件 ID。
    猜你喜欢
    • 2013-09-24
    • 1970-01-01
    • 2015-09-18
    • 2018-10-03
    • 2017-03-07
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    相关资源
    最近更新 更多