Function 模板中的Meteor变量而不是函数

Function 模板中的Meteor变量而不是函数,function,templates,variables,meteor,spacebars,Function,Templates,Variables,Meteor,Spacebars,Meteor spacebars中是否有存储模板函数返回值的方法? 我会解释得更清楚 例如,假设我需要知道某个事件是否已启动。 在client.js上,我将有: Template.event.isEventStarted = function(eventId) { var event = Events.findOne({_id: eventId}); return Events.isStarted(event); } 假设现在我需要多次从“事件”模板访问“isEventStar

Meteor spacebars中是否有存储模板函数返回值的方法? 我会解释得更清楚

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

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

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

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

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

通过这种方式,您可以确保只有在相应的数据发生更改时才会重新计算查询。

我似乎无法访问emboxValue函数中的集合。所以我尝试将我的集合文件移动到“lib”文件夹下,我可以在函数中使用“Events”集合。但“eventId”一直是“未定义的”。我使用{{#if isEventStarted _id}从模板传递事件id。