user1427835
user1427835

Reputation: 31

Meteor variable in template instead of function

Is there a way in Meteor spacebars to store a template function return value? I'll explain better.

Suppose, for instance, I need to know if an event is started. On client.js I will have:

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

Suppose now I need to access "isEventStarted" from the "event" template many times. I will need also to access it from subtemplates. The query is obviously executed everytime "isEventStarted" is called, so does the function "isStarted". They execute client side, but I could potentially have many events templates with many subtemplates.

Upvotes: 1

Views: 95

Answers (1)

Tomasz Lenarcik
Tomasz Lenarcik

Reputation: 4880

That's basically what UI.emboxValue is designed for. Try doing this:

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

This way you can be sure the query will only be recomputed when the corresponding data changes.

Upvotes: 1

Related Questions