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

时间:2014-06-14 10:32:58

标签: function templates variables meteor spacebars

Meteor空格键中是否存在存储模板函数返回值的方法? 我会更好地解释。

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

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

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

1 个答案:

答案 0 :(得分:1)

这基本上是UI.emboxValue的目的。试着这样做:

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

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