从Ember.Route中访问参数的正确方法是什么? setupController? [英] What's the proper way to access parameters from within Ember.Route. setupController?

查看:107
本文介绍了从Ember.Route中访问参数的正确方法是什么? setupController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ember.Route.model 可以访问 params 变量,但 Ember。 Route.setupController 不。这对我来说很麻烦,因为我的路径有多个动态细分,我需要在我的模板中使用它们。

Ember.Route.model has access to the params variable, but Ember.Route.setupController does not. This is troublesome for me, because my path has multiple dynamic segments, and I need to use all of them in my template.

具体来说,我的路径如下所示: code> /项目/:PROJECT_ID /任务/:TASK_ID 。请注意,任务可以属于许多项目,而不仅仅是一个。因此,我们无法知道我们正在查看的项目只是在查看任务本身:我们必须使用URL中找到的项目ID。这是我现在正在做的:

Specifically, my path looks like this: /project/:project_id/task/:task_id. Note that a task can belong to many projects, not just one. Therefore we can't tell what project we're looking at just be looking at the task itself: we have to use the project ID found in the URL. Here's how I'm doing it currently:

App.TaskRoute = Ember.Route.extend({

  // This works just fine:
  serialize: function(model) {
    return {
      task_id: model.get('id'),
      project_id: this.modelFor('project').get('id')
    };
  },

  model: function(params) {
    this.set('parentProjectId', params.project_id);

    return App.Task.find(params.task_id);
  },

  setupController: function(controller, model) {
    var parentProject = this.modelFor('project') ?
                        this.modelFor('project') :
                        App.Project.find(this.get('parentProjectId'));
    controller.set('parentProject', parentProject);

    controller.set('content', model);
  }
});

也许我是偏执狂,这只是感觉到黑客。如果路由意图访问参数,那么它将已经有一个附加到它的 params 属性。有没有更好的方法?

Maybe I'm being paranoid, this just feels hacky. If the route was meant to have access to the parameters, then it would already have a params property attached to it. Is there a better way?

编辑:我对上面的代码做了一些更新。另外,我的路线如下所示:

I made some update to the code above. Also, my routes look like this:

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


推荐答案

您无法访问setupController钩子中的这些参数。

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.

您的代码看起来很不错,因为模型挂钩可以访问params对象,因为它刚刚被调用。好的,你真的知道,你想这样做。对你有什么好感觉?当看这个代码时,我问自己为什么你没有把你的路由逻辑分解成一个ProjectRoute和一个从属的TaskRoute。不会为你工作吗?

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?

更新:对您的更改的回应

嵌套资源可能是您的案例成功的关键:

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' });
  });
});

由于TaskRoute嵌套不需要重命名为ProjectTaskRoute:

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

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

这应该可以删除 parentProjectId属性路线。

这篇关于从Ember.Route中访问参数的正确方法是什么? setupController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆