简体   繁体   中英

How to evaluate variables on handlebars helpers

I'm trying to build a helper to resolve ulr routes, like this:

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

And i call this helper like this:

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

I need to evaluate "membership.firm.id" but i alaways get "Uncaught TypeError: Object # has no method 'fn' ". What is the right way to do this?

I got make it work, thanks! ;)

my template

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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