2
votes

How do I dynamically get a route's parent route's model. Say my router looks like this:

App.Router.map ->

  @resource 'farms', ->
    @resource 'farm', path: '/:farm_id', ->
      @resource 'attachments'

  @resource 'fields', ->
    @resource 'field', path: '/:field_id', ->
      @resource 'attachments'

If I have 2 routes: App.FieldRoute and App.AttachmentsIndexRoute, and I want to get the Field model from App.AttachmentsIndexRoute, I could do:

model: ->
  @modelFor('field')

How do I do the same thing dynamically, not specifying the model's route explicitly? In other words, if I'm looking at attachments for a Farm instead, the same code should work.

(Isn't there a way in Ember to grab the parent route from a nested route?)

UPDATE:

Doesn't look like there's a clean (API) way to do this. I found some hacks using internal properties like router.currentHandlerInfos. Might have to go that route until Ember team adds a proper way of doing this.

1

1 Answers

0
votes

There is no API for that, and I think it will stay that way.

A clean workaround would be to reopen the Route and add this functionality.

See here: Getting parent route in EmberJS