我有一个带有 lambda 的 mustache 模板,如下所示:
{{#myfunc}}myvalue{{/myfunc}}
它由 hogan.js 预编译如下:
define(['hogan'],
function (Hogan) {
var template = new Hogan.Template(function (c, p, i) {
var _ = this;
_.b(i = i || "");
if (_.s(_.f("myfunc", c, p, 1), c, p, 0, 11, 18, "{{ }}")) {
_.rs(c, p, function (c, p, _) {
_.b("myvalue");
});
c.pop();
}
return _.fl();;
});
return function (context, partial, indent) {
return template.render(context, partial, indent);
};
});
我使用 Marionette.ItemView 渲染模板,将 lambda 函数传递给 Backbone.Model,如下所示:
myfunc: function (key) { console.log("key", key); }
奇怪的是:函数 myfunc 将被调用并记录到控制台,但模板没有向它传递 key 。 我读到 Hogan 在预编译模式下不支持 Lambda(大约一年前 - 我想这是固定的) - 但如果是这样,它是怎么发生的,myfunc 被调用了?
我对我的 vendor/hogan.js 库进行了一些调试 - 看起来 hogan 看不到 lambda 标签之间的值(此处:myvalue)。
有人以前见过这个吗?
最佳答案
我的调查:
只有在使用诸如 grunt-hogan 或 grunt-templates-hogan 之类的 grunt 插件时才会出现此问题。
如果在脚本中使用 Hogan.compile()
内联渲染之前编译模板,问题就解决了。
我在 github 上创建了一个小项目,因为 jsfiddle 不允许使用 grunt 并将其链接到问题 https://github.com/twitter/hogan.js/issues/225 .
项目:https://github.com/ssuvorov/hogan-issue
我的解决方案是用必要的对象字段替换 lambda。我的意思是在渲染之前准备包含所有必要信息的数据。
关于javascript - Hogan 在预编译模式下不支持 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15377481/