Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global helper is overriding local context in meteor handlebars template

Update: The bug logged in the selected answer has been fixed by the meteor devs

In my meteor app I have some coffescript making a global helper:

Handlebars.registerHelper "title",->
    Session.get("title")

and a piece of template:

{{#each edited}}
    <li><a class="left-nav" href="{{entryLink this}}">{{this.title}}</a></li>
{{/each}}

this.title is being overridden by the global helper rather than using the this.title context. (I know because if I remove the global helper it works great)

If I add the following:

Handlebars.registerHelper "debug", ->
    console.log this
    console.log this.title

to the template like this:

{{#each edited}}
    <li><a class="left-nav" href="{{entryLink this}}">{{this.title}}{{debug}}</a></li>
{{/each}}

this.title prints to the console correctly, but is not inserted into the template

Any idea why this is happening or how to make the "this.title" be from the local context

like image 730
funkyeah Avatar asked Oct 03 '22 23:10

funkyeah


2 Answers

Looks like a bug to me, since https://github.com/meteor/meteor/wiki/Handlebars#expressions-with-dots says:

Paths starting with this always refer to properties of the current data context and not to helpers.

I submitted an issue for it: https://github.com/meteor/meteor/issues/1143

like image 97
Andrew Wilcox Avatar answered Oct 18 '22 00:10

Andrew Wilcox


I read something about this on the handlebarsjs.com website today. Give this a try:

Handlebars also allows for name conflict resolution between helpers and data fields via a this reference:

{{./name}} or {{this/name}} or {{this.name}}

Any of the above would cause the name field on the current context to be used rather than a helper of the same name.
like image 26
Michael Avatar answered Oct 18 '22 00:10

Michael