DEV Community

Sivasubramanian
Sivasubramanian

Posted on

Passing pre-computed routes to link-to helper in Ember

Syntax

{{link-to routeName  dynamicSegment1 dynamicSegment2 (query-params key1=value1 key2=value2)}}

(or)

{{link-to params=linkParameters}}
Enter fullscreen mode Exit fullscreen mode

Description

Consider a photo application with below routes

this.route('photos', function() {
  this.route('resolution', {
    path: '/:value'
  });
});
Enter fullscreen mode Exit fullscreen mode

Compute links in component

linkParametersVariation1: computed({
  get() {
    let resolution = get(this, 'resolution);
    return { 'routeName': 'photos/resolution', dynamicSegment: resolution, page: 1, perPage: 30 }
  }
}),

linkParametersVariation2: computed({
  get() {
    let resolution = get(this, 'resolution);
    return ['photos/resolution', resolution, { isQueryParams: true, values: { page: 1, perPage: 30 } }]
  }
})
Enter fullscreen mode Exit fullscreen mode

Using links in template

{{#link-to linkParametersVariation1.routName linkParametersVariation1.dynamicSegment (query-params page=linkParametersVariation1.page perPage=linkParametersVariation1.perPage)}}
  <div>variation1</div>
{{/link-to}}

{{#link-to params=linkParametersVariation2}}
  <div>variation2</div>
{{/link-to}}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)