简体   繁体   English

全球助手正在重写流星车把模板中的本地环境

[英]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: 在我的流星应用程序中,我有一些coffescript作为全球帮手:

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. this.title被全局帮助器覆盖,而不是使用this.title上下文。 (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 this.title正确打印到控制台,但未插入模板

Any idea why this is happening or how to make the "this.title" be from the local context 知道为什么会这样或者如何使“this.title”来自本地环境

Looks like a bug to me, since https://github.com/meteor/meteor/wiki/Handlebars#expressions-with-dots says: 看起来像是一个bug,因为https://github.com/meteor/meteor/wiki/Handlebars#expressions-with-dots说:

Paths starting with this always refer to properties of the current data context and not to helpers. this开头的路径始终引用当前数据上下文的属性,而不是辅助对象。

I submitted an issue for it: https://github.com/meteor/meteor/issues/1143 我提交了一个问题: https//github.com/meteor/meteor/issues/1143

I read something about this on the handlebarsjs.com website today. 我今天在handlebarsjs.com网站上看到了这个。 Give this a try: 尝试一下:

Handlebars also allows for name conflict resolution between helpers and data fields via a this reference: Handlebars还允许通过此引用解析帮助程序和数据字段之间的名称冲突:

{{./name}} or {{this/name}} or {{this.name}} {{。/ name}}或{{this / name}}或{{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. 上述任何一个都会导致使用当前上下文中的name字段而不是同名的帮助程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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