简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM