I'm working on this web app, using Expressjs, and I feel the need for a feature that Microsoft's Razor view engine provides.
Razor allows one to call helpers from within other helpers. In Expressjs, the helpers' scope does not extend to the helpers themselves when they are called from a view.
Is there a sollution for this? I would like to not have to pass the helpers to the view as parameters, or have to require the helper's file.
Thanks in advance
Example:
I have
app.helpers({ TextBoxHelper: require('./TextBox.js') });
app.helpers({ CharCounter : require('./CharCounter.js' ) });
in my server. When I'm rendering a view, I use jshtml view engine and to call a helper, I just have to do the following:
@TextBoxHelper() //I can do this because all helpers are present in this scope
When the helper is called, it exits the view scope, and there will be no access to the other helpers there.
Now, I would like this helper "TextBox" to call "CharCounter". This second helper, is supposed to be called by several other helpers, so, it would be great if I could just call it from the helpers' ".js" files, without having to require the script, or send it as a parameter.
I would just bundle all the helpers into one object. That way you can use this.someHelper()
.
var helpers = {
first: function () {
console.log('first');
helpers.second();
},
second: function () {
console.log('second');
}
};
helpers.first();
If this
isn't what you want it to be you can always easily change it in Javascript.