how to create expander dynamically?

What are some ways to dynamically create Meteor Handlebars template helpers?

  • This is purely a programming question, however StackOverflow doesn't have an "ask to answer" feature, so I'm asking this here as well. I'm also curious whether Quora could work for this kind of questions or not. Q: I have a list of i18n translation strings within an object that I'm iterating over. Instead of creating a Template Helper for each string manually though, which would soon become redundant and repetitive, I would like to create the Helpers dynamically, within a loop, like so: for (var namespace in Meteor.i18nMessages) {   for (var msg in Meteor.i18nMessages[namespace]){     //Template[namespace][msg] = __(namespace + "." + msg); // <- works but is not reactive     Template[namespace][msg] = function() { // <- Doesn't work: always returns last value from object       return __(namespace + "." + msg);     }   } } However when I do, I lose reactivity. How would one go about solving this? I'm a fan of best-practices and elegant code. http://stackoverflow.com/questions/16315122/dynamically-create-meteor-handlebars-template-helpers

  • Answer:

    This solution is not pretty but it's working. for (var namespace in Meteor.i18nMessages) {   var obj = {};   for (var msg in Meteor.i18nMessages[namespace]) {     var str = 'obj["' + msg + '"] = function() { return __("' + namespace + '.' + msg + '"); }';     console.log(str);     eval(str);   }   Template[namespace].helpers(obj); }

Kris Haamer at Quora Visit the source

Was this solution helpful to you?

Other answers

I won't show you any code, but cmather did that in mini-pages package: https://github.com/cmather/meteor-mini-pages/blob/master/lib/helpers.js It must be useful :) And it's reactive.

Gabriel H Pugliese

Right now, both Meteor router and mini page router are merged into Iron-router.  Do use that.  http://www.github.com/EventedMind/meteor-iron-router

Bozhao Yu

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.