1
votes

I want to use both a parent route's model, and the child route's model from the child route. Not sure the "Ember way" to go about this.

To be a little more specific:

The parent

The parent route has a group model and has the dynamic route groups/:group_id

The child

Each group works on several units. In the template for the group route above, the list of units is displayed, each with a link to the child route in question, groups/:group_id/unit/:unit_id. The model for this route is the unit, set by passing the unit as an argument in the link-to helper in the parent route template.

The problem

The problem is that, in addition to the unit model, I also need to be able to access the parent model - the group, from within the groups/:group_id/unit/:unit_id route.

Its also a one way relationship. While a group has several units, any other group might also have any number of units, so I can't use the relationship.

I might set-up an hash to return both models in the child route.js, but the model hook wouldn't be executed if the model is passed via the link-to. Stuck on exactly how to do this. Or the other way is to maybe access the parent controller from the child controller, but not sure the correct way to do this.

Thanks

1
Thanks also for the comment suggesting use of the modelFor route method. As perfect as it seems, can't make use of it in this case.rjoxford

1 Answers

0
votes

It doesn't feel quite right, but I've gotten around the problem by injecting the parent controller.

I've gone for this in the groups/:group_id/unit/:unit_id controller.

import Ember from 'ember';

export default Ember.Controller.extend({

    groupController: Ember.inject.controller('groups.group'),
    group: Ember.computed(function(){
        return this.get('groupController').get('model');
    }),
});

Nevertheless feel there ought to be a better way.

I'd like:

  1. Ideally, I’d like to reopen the model in the route, and hash the parent model together with the child model.

  2. Alternatively, I’d like to be able to access the parent model from the child controller, in a similar way to modelFor in the route.

Either of these would allow me to make use of the dynamic segments in the URL.