Search code examples
meteorhandlebars.jsmeteor-helper

Meteor template helpers accessed on each call


I've read this Meteor template helpers fire multiple times and tried to reproduce this without any collections involved.

So I got helper like this:

Template.myTemplate.helpers({
    test: function(){
        console.log("Fired");
        return {
            name : "Foo"
        };
    }
});

And template looks like this:

<template name="myTemplate">
    {{test.name}}{{test.name}}
</template>

In console i expected to see "Fired" only once, but helper is called actually twice ? So if my helper is accessing collection and getting data from there, every time i use {{helper.key}} it performs a query? is this how it should work ?


Solution

  • Yes you are right the template system will call a helper for each time you use it in your template.

    I guess what you're asking is if this is efficient for collections since they're called again and again.

    This is why meteor's .find() returns a cursor which is a sort of cache. You can see this if you try and find something with .find(), you would get an object but not actually your data. The cursor only fetches data as it is needed so it is not as inefficient as it may seem.

    Appending .fetch() actually fetches the data and does not use this cache which is why it is less efficient and often suggested not to be used as the return value of the helper.