Eswar
Eswar

Reputation: 77

How to pass dynamic routes params in ember link-to

I have two routes in my router.js

'user/:id' -> will move to user show page 
'user-answer/:id/answers' -> will move to user answer page 

currently i am using a if condition to change the link as shown in below code ,

sidePanel.hbs

{{each users as |user|}}
{{#if (eq routesValue user-answer)}}
    {{#link-to user.name user.id answer}}
      {{user.name}}
    {{/link-to}}
{{else}}  
    {{#link-to user.name user.id}}
      {{user.name}}
    {{/link-to}}
{{/if}}
{{/each}}

I use the sidePanel component in other two components as

{{sidePanel routesValue='user-answer' answer=answer users=users}}
{{sidePanel routesValue='user' users=users}}

it is working fine , but is there a way to use link-to one time , instead of a conditional check , like passing dynamic route parameters ?

something like

 {{each users as |user|}}
        {{#link-to user.name user.id optionalParam=answer}}
          {{user.name}}
        {{/link-to}}
    {{/each}}

Upvotes: 0

Views: 397

Answers (1)

Lux
Lux

Reputation: 18240

The <LinkTo> form takes a @models argument that takes an array:

<LinkTo @route="routename" @models={{this.models}}>

this should make it easy to compute an array in js:

get models() {
  const result = [this.user.id];
  if(this.answer) {
    result.push(this.answer);
  }
  return result;
}

Upvotes: 1

Related Questions