template - spacebar meteor

Meteor use fetch or find in template helper functions? (2)

This is what we follow in Oodles Technologies.

For defining helper just go to your template js file for example if you have a template name as allInventory so just go to allInventory.js file and write the helper as follows:-



make a function inside this helper in which you put your logic for getting data from Database or Session or from other service and than use that in you html like:-

        productDetails: function() {
              return Session.get('dbData');

On html side you just need to use the function name as follows:-

{{#each productInfo in productDetails}}

        <div class="imgb"><img src="{{productInfo.image_url}}"></div>

<a type="button" class="full-view text-success"><i id="fullView" data="{{productInfo._id}}" class="fa fa-eye"></i></a>


As you can see in above productDetails a function name in your helper class on which you get the data you want to render on your Html is accessible directly via that name and you can traverse that via each loop in html templates.

Inside a meteor template helper function, is there any difference in performance, number of re-renders, or anything else if I return the result of a find vs a fetch?

For example, the find approach:

Template.players.topScorers = function () {
  return Users.find({score: {$gt: 100}}, {sort: {score: -1}});

Or adding a fetch:

Template.players.topScorers = function () {
  return Users.find({score: {$gt: 100}}, {sort: {score: -1}}).fetch();

The find-only approach is what is currently in the docs, but I’ve seen lots of other people using fetch.

Yes there is.

By using fetch you register a dependency on the entire query result set on the spot. By using find and later on iterating using {{#each}} a dependency is registered on every document separately. So when one document changes, only the relevant code is re-rendered. When using fetch, changing any document in the result-set would re-render the entire scope in which you used fetch.

For small result-sets it doesn't make any difference. For larger sets with frequent changes it could slow down computation and cause undesired visual artefacts.

I wrote a post which may help you understand it (it doesn't answer your question directly though)