0

I'm using js-routes for Rails and would like to use this in combination with Ember.js. I wrote a helper to fetch the routes from the global Routes as provided by js-routes, but I can't seem to get it working.

Usage: {{route 'user_path' user.id}}

Expected return: /users/123

Attempt 1:

Ember.Handlebars.registerHelper('route', function(path, id) {
    console.log(path); // returns the path as a string --> OK!
    console.log(id); // returns the string 'user.id' --> Not ok!
    return Routes[path](id);
});

Attempt 2:

Ember.Handlebars.registerBoundHelper('route', function(path, id) {
    console.log(path); // returns undefined --> Not OK!
    console.log(id); // returns 123 --> OK!
    return Routes[path](id);
});

How can I achieve this? Thanks!

4

1 回答 1

1

Never mind, I found it myself:

Ember.Handlebars.registerBoundHelper('route', function(context, options) {
    return Routes[options.hash.path](context.id);
});

Usage: {{route this path='user_path'}}

于 2013-07-24T11:26:03.583 回答