What's the proper way to access parameters from within Ember.Route. setupController?

后端 未结 2 995
谎友^
谎友^ 2021-02-07 06:44

Ember.Route.model has access to the params variable, but Ember.Route.setupController does not. This is troublesome for me, because my path

相关标签:
2条回答
  • 2021-02-07 07:30

    Since Ember 1.8, the Route class has a paramsFor function:

    import Route from '@ember/routing/route';
    
    export default Route.extend({
        setupController(controller) {
            this._super(...arguments);
            const params = this.paramsFor('name.of.your.route')
        }
    });
    
    0 讨论(0)
  • 2021-02-07 07:37

    You have no access to these params in the setupController hook. The model hook has access to a params object, because it is just called, when your app is entered via URL.

    Your code looks quite fine, it you really know, that you want to do it this way. What does feel hacky to you about it? When looking at this code, i am asking myself why you did not split the logic of your Route into a ProjectRoute and a subordinated TaskRoute. Wouldn't that work for you?

    Update: Response to your changes

    Nesting resources is likely the key to success in your case:

    App.Router.map(function() {
      this.resource('project', { path: '/project/:project_id' }, function(){
        this.resource('task', { path: '/task/:task_id' });
      });
    });
    

    Since the TaskRoute is nested not you have to rename it to ProjectTaskRoute:

    App.ProjectTaskRoute = Ember.Route.extend({
    ...
    });
    

    This should enable you to remove the parentProjectId property from the Route.

    0 讨论(0)
提交回复
热议问题