如何评估车把助手上的变量

时间:2013-05-07 07:57:09

标签: javascript handlebars.js

我正在尝试构建一个帮助程序来解析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”,但我总是得到“未捕获的TypeError:对象#没有方法'fn'”。这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我让它工作,谢谢! ;)

我的模板

<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>

我的助手

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 "#";
});