EmberJS如何为路由选择模板? [英] How does EmberJS select template for a Route?
问题描述
我定义了这些路线:
this.resource('projects', function() {
this.resource('project', { path: ':project_id'}, function() {
this.route('details');
this.route('members');
});
});
我以为是通过project.details路由寻找project / details模板。它确实但奇怪的是它没有得到正确的模型。请参阅 http://jsbin.com/ELaxigE/19/edit
What I thought was that by convention project.details route would look for "project/details" template. It does but strangely it does not get the correct model. See http://jsbin.com/ELaxigE/19/edit
现在,如果我创建项目模板,那么现在,而不是提供项目/细节模板,那么它可以工作。请参阅 http://jsbin.com/ELaxigE/21/edit
Now instead of providing "project/details" template if I create "project" template then it works. See http://jsbin.com/ELaxigE/21/edit
我很困惑发生了什么。有人可以解释吗?
I am confused what is happening. Can someone explain?
推荐答案
给定路由。当模型钩子不被定义,并且具有以 _id
结尾的动态段:
Given a route. When the model hook ins't defined, and have a dynamic segment that ends with _id
:
this.route('edit', { path: ':user_id' });
这将生成一个这样的路由:
This will generate a route like this:
App.EditRoute = Ember.Route.extend({
model: function(params) {
return App.User.find(params.id);
}
});
在您的情况下,唯一的动态分段路由是项目
,因为:project_id
。
In your case the only dynamic segmented route is project
, because the :project_id
.
this.resource('project', { path: ':project_id'}, function() { ... });
所以因为细节
和成员
,只是正常的路线,它没有一个模型
。
So because details
and members
, are just normal routes, it doesn't have a model
.
当您将模板项目/细节
更改为项目
时,事情的工作原理是:
When you change the template project/details
to project
, the things work because:
您转换到 project.details
,首先转换到项目
路由,因为您已声明 this.resource('project'...)
。并且因为它是一个动态分段路由,所以返回 App.Project
实例,并将您的模板绑定到此模型
。
You transition to project.details
, first is transitioned to project
route, since you have declared this.resource('project'...)
. And because it's a dynamic segmented route, the App.Project
instance is returned, and the your template is rendered bound to this model
.
此后,子路由 project.details
已转换,但这次,模板 project.details
不存在。所以没有什么可以渲染
After this, the child route project.details
is transitioned, but this time, the template project.details
not exist. So nothing is rendered.
我认为这些解决方案是@alexspeller答案,或者:
I think that the solutions are the @alexspeller answer, or:
this.resource('project', function() {
this.route('details', { path: 'details/:project_id' });
this.route('members', { path: 'members/:project_id' });
});
我希望它有帮助。
这篇关于EmberJS如何为路由选择模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!