I've defined a template helper in Meteor, say
Template.postsList.helpers({
filteredPosts: function getPosts() {
return Posts.find(...);
}
});
How can I debug that template helper from the console, and how can I reuse it from other code in the app?
It does not formally answer the question but I would like to share a simple technique, which virtually solves most of the problems you described.
Let's say we have a group of helpers, which we want to be able to access from different parts of your application both from templates and directly from our javascript code. To achieve that, I would have a global
Helpers
object, to which I could attach as many function as I want, e.g.This makes them easily accessible throughout the code and pretty easy to test as well. But I also want to use them in my Spacebars templates, right?
A simple idea is to create a single global helper that would return the
Helpers
object itself.The nice thing about this is that it will enforce me to prefix each reference to my "global helper" with
$
, i.e.{{$.year}}
or{{$.routeIs 'home'}}
, which makes the code a lot more readable.Unfortunately, there's a small problem with that solution. Consider the following example:
The problem is that if my helper is about to access the current context through
this
, it will get theHelpers
object itself rather than the data context, so the{{$.fullName}}
will never work properly. But of course, there's a workaround for that:EDIT
I've added a reference implementation here:
https://github.com/anticoders/meteor-helpers
Wanting to call the helper from elsewhere in the app suggests that you should factor it out in a function.
To quickly debug the helper, evaluate this in the client console:
There is a package that might help with debugging templates and template helpers: prasad19sara:client-debugger.