Search code examples
ember.jsember-router

Resource with dynamic segment and retrieving the model in a nested route


I have the following route :

@resource 'groups', {path: 'events/:event_id/groups'}, ->
  @route 'new'

In my new route, in order to build a new App.Group object, I need to know which is the event we're in.

So I have the following route, which corresponds to the resource :

App.GroupsRoute = Ember.Route.extend
  model: (params) ->
    App.Event.find params.event_id

And the one which corresponds to the route :

App.GroupsNewRoute = Ember.Route.extend
  model: ->
    App.store.createRecord App.Group

I would need to specify the group's event when creating it. However, in the GroupsNewRoute, I don't seem to be able to retrieve the event. The params don't include event_id.

How can I get the model from a parent resource in a route ?


Solution

  • There are a few different issues here:

    You should not use App.store. The store should be available on routes as this.get('store').

    I'm not sure why you're naming your route GroupsRoute. Since it represents an event, you should name it EventRoute. This is not required, but it would be more idiomatic. Also, you don't need to specify the model for GroupsRoute. What you are doing is the default behavior.

    Finally, you can get the model for another route via: this.modelFor('groups') in your route. That should solve your issue here.