更新:所选答案中记录的错误已由 meteor 开发人员修复
在我的 meteor 应用程序中,我有一些制作全局助手的 coffescript:
Handlebars.registerHelper "title",->
Session.get("title")
和一个模板:
{{#each edited}}
<li><a class="left-nav" href="{{entryLink this}}">{{this.title}}</a></li>
{{/each}}
this.title 被全局助手覆盖,而不是使用 this.title 上下文。 (我知道,因为如果我删除全局助手,它会很好用)
如果我添加以下内容:
Handlebars.registerHelper "debug", ->
console.log this
console.log this.title
像这样的模板:
{{#each edited}}
<li><a class="left-nav" href="{{entryLink this}}">{{this.title}}{{debug}}</a></li>
{{/each}}
this.title 正确地打印到控制台,但没有插入到模板中
任何想法为什么会发生这种情况或如何使“this.title”来自本地上下文
最佳答案
自 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.
我为此提交了一个问题:https://github.com/meteor/meteor/issues/1143
关于javascript - 全局助手正在覆盖 meteor handlebars 模板中的本地上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16880462/