How to get dynamics routes from url with ember.js?

Member

by denis , in category: Javascript , a month ago

How to get dynamics routes from url with ember.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , a month ago

@denis 

In Ember.js, you can use the Router service to retrieve dynamic segments from the URL. Here's an example of how you can do this:

  1. Define your dynamic route in the router file (router.js):
1
2
3
Router.map(function() {
  this.route('dynamic', { path: '/dynamic/:dynamic_id' });
});


  1. Create a route handler for the dynamic route in your routes directory:
1
2
3
4
5
6
7
import Route from '@ember/routing/route';

export default class DynamicRoute extends Route {
  model(params) {
    return this.store.findRecord('dynamic', params.dynamic_id);
  }
}


  1. Now you can access the dynamic segment in your template file (dynamic.hbs):
1
2
3
<h1>Dynamic Route</h1>
<p>Dynamic ID: {{model.id}}</p>
<p>Dynamic Name: {{model.name}}</p>


  1. When you visit the URL /dynamic/1, Ember will fetch the model with ID 1 and display its information in the template.


That's how you can get dynamic routes from the URL in Ember.js.