Dynamic segments using Model's Data


#1

I’m trying to access the model’s data using the dynamic segments. How to go about this ? Can the data that is present in the route’s model can be accessed here with the :/id url ?

The data from the route’s model

The route file :


#2

if you’re asking how you can get the ‘id’ param in the model hook, the model hook takes two parameters: params, and transition. The id will be one of the params passed in, e.g.:

import Route from '@ember/routing/route';

export default Route.extend({
  model(params) {
    return this.store.findRecord('contact', params.id);
  }
});

#3

Thanks for your reply.

The data is in the Route’s model itself as in the picture above and not in the store. How to fetch the data for id:1 when the URL /contact/1 is hit ?


#4

Well you haven’t specified which route it’s in… I’ll assume it’s the same route. If that’s the case I’d define that array that it’s currently returning as a const outside of the route module and just return contacts[params.id] from the model hook.

If that screenshot you posted is of a different route you can get it via let contacts = this.modelFor('other-route') and then return contacts[params.id] but note that unless “other route” is a parent route it is not guaranteed to have a resolved model yet.


#5

It worked.

Thanks a lot !