我正在尝试构建一个帮助程序来解析 ulr 路由,如下所示:
Handlebars.registerHelper('rails_route', function(route, options) {
var fn = Routes[route];
if(typeof fn === 'function') {
var opts = {};
for (var key in options.hash) {
var value = options.hash[key];
opts[key] = options.fn(value); // Uncaught TypeError: Object #<Object> has no method 'fn'
}
return fn(opts);
}
return "#";
});
我这样称呼这个助手:
<ul class="dropdown-menu">
{{#each membership in controllers.currentUser.user.memberships}}
<li>
<a href="{{rails_route root_path firm_id=membership.firm.id }}">{{membership.firm.name}}</a>
</li>
{{/each}}
</ul>
我需要评估“membership.firm.id”,但我总是得到“Uncaught TypeError: Object # has no method 'fn'”。这样做的正确方法是什么?
最佳答案
我已经成功了,谢谢! ;)
我的模板
<ul class="dropdown-menu">
{{#each membership in controllers.currentUser.user.memberships}}
{{#with membership.firm}}
<li>
<a href="{{rails_route root_path firm_id=id }}">{{name}}</a>
</li>
{{/with}}
{{/each}}
</ul>
我的 helper
Handlebars.registerHelper('rails_route', function(route, options) {
var fn = Routes[route];
if(typeof fn === 'function') {
var opts = {};
for (var key in options.hash) {
var value = options.hash[key];
opts[key] = this.get(value);
}
return fn(opts);
}
return "#";
});
关于javascript - 如何评估 Handlebars 助手上的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16414017/