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

狂风中的少年 提交于 2019-12-03 05:01:38

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.

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')
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!