EmberJS - How to dynamically generate link with linkTo?

后端 未结 3 824
南笙
南笙 2021-01-18 09:13

Is there a way to dynamically generate a link using the link-to helper by passing a variable with the route path?

For example, instead of hard coding the path like t

相关标签:
3条回答
  • 2021-01-18 09:31

    Is there a way to dynamically generate a link using the linkTo helper by passing a variable with the route path?

    Not at the moment.

    The use case for this is to allow me to consolidate templates which only differ by this path. For instance, if there are two collections each with a different destination.

    Agreed this is a valid use case, I would expect linkTo helper to support it in future. Meantime since you've only got two collections you could accomplish this with conditionals in the template

    {{#if isRouteOne}}
      {{#linkTo "routeOne.subrouteOne" model}}{{model.title}}{{/linkTo}}
    {{else}}
      {{#linkTo "routeTwo.subrouteTwo" model}}{{model.title}}{{/linkTo}}
    {{/if}}
    
    0 讨论(0)
  • 2021-01-18 09:35

    Instead of {{linkTo}},you can use {{action}} and handle the event of transition by route name specify in destination property of model.

    0 讨论(0)
  • 2021-01-18 09:48

    There is another very similar SO Question in which I answer this question. The solution is possible using the inline version of the link-to helper.

    In this situation, we would have:

    {{link-to model.title destination model}}
    

    Which would be compiled to:

    <a href="#/route/subroute/3856">
      Click me
    </a>
    
    0 讨论(0)
提交回复
热议问题