What to do if you need a function both in javascript and as a template helper


#1

Is this a good practice?

dictionaryLookup = function (lookupType, index) {
  var dict = ChartDictionary.findOne({"lookup_type": lookupType, "lookup_id": index});
  return dict.lookup_label;
}

Template.registerHelper('dictionaryLookup', function (lookupType, index) {
  return dictionaryLookup(lookupType, index);
});

This allows me to use dictionaryLookup as a javascript function when needed, and also to use dictionaryLookup as a template helper in my templates…

Reason being, I’m moving from building my own tables in the template with an EACH block, to using https://github.com/aslagle/reactive-table and I’m finding that things that used to be template helpers now need to be javascript functions.


#2

Sounds like viewmodel would be solving this for you, you’d put your logic inside the viewmodel hand have it send its results to your table.


#3

Yep, this is good practice, and you could probably write the registering like this :

Template.registerHelper('dictionaryLookup', dictionnaryLookup);

#4

I thought I tried something like that earlier, but when I tried it now, it worked fine, thanks for the suggestion @vjau