So let’s say I have a whole bunch of templates loading on the screen simultaneously, and they all need access to some data. The data rarely changes and doesn’t need to be reactive (let’s assume we’re guaranteed it won’t change while these templates exist).
What’s the best practice for sharing that data among the templates?
Option 1: Every helper in every template pulls the data from Mongodb. So in every helper we have:
Fruit.find({tasty: 1}).fetch();
Option 2: Every template pulls the data from Mongodb, saves to the template instance, and shares it with it’s own helpers. So in every Template we have:
Template.instance().tastyFruit = Fruit.find({tasty: 1}).fetch();
and then in its helpers we access that property.
Option 3: Pull the data from mongo once in a top-level layout template and pass it to all the sub-templates:
{{> subTemplate1 getTastyFruit}}
Option 4: Pull the data from mongo once in a top-level layout template, save it to the template instance, then call that from sub-templates (either in each helper, or resave in each sub-template during .onCreated()
); Like:
Template.instance().tastyFruit = Template.instance().parentView.tastyFruit
Option 5: Save the data in a session variable (if it’s JSON-able) and access that in each template/helper.
Session.set('tastyFruit', ...);
Option 6: Create a variable in the global namespace, save the value to it in the top-level layout .onCreated()
method and then access elsewhere.
var tastyFruit = null;
Template.mainLayout.onCreated(function(){tastyFruit = ...;});
Template.subTemplate1.helpers({eatThis: () => tastyFruit,});
Are there options I missed?
What’s the right way to do this in meteor/blaze?
Why?