1

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

例如,假设我需要知道一个事件是否开始。在 client.js 上,我将拥有:

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

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

4

1 回答 1

1

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

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

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

于 2014-06-14T11:29:07.420 回答