[英]EmberJS 2 route dynamic segment in not nested routes undefined
Really new to ember and trying to setup basic (in my mind) routes. 对炭烬来说真的很新,并且尝试设置基本的路线(在我看来)。
I have calendars
resource and I want to display individual calendars. 我有
calendars
资源,我想显示单个日历。
My app/router.js
has the following: 我的
app/router.js
具有以下内容:
this.route('calendar', {path: 'calendars/:calendar_id'}, function () {
this.route('show');
this.route('edit');
});
this.route('calendars', function(){
this.route('create');
});
Folders are as following: 文件夹如下:
app/routes: [
calendars: [create, index],
calendar: [edit, show]
]
app/templates: [
calendars: [create, index]
calendar: [edit, show]
]
In app/routes/calendar/show.js
: 在
app/routes/calendar/show.js
:
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
return this.store.findRecord('calendar', params.calendar_id);
}
});
Problems start when I go to http://SERVER/calendars/5/show (5 is a :calendar_id part, SERVER is what hosts ember app) : 当我转到http:// SERVER / calendars / 5 / show (5是:calendar_id的一部分,SERVER是托管ember应用程序的部分)时,问题开始了:
model()
function in app/routes/calendar/show.js
file. app/routes/calendar/show.js
文件中的model()
函数,都会发生这种情况。 But I don't know where that call to the server happens: 但是我不知道对服务器的调用发生在哪里:
model(){}
altogether, my template renders model record (the calendar record that Ember fetches). model(){}
,则我的模板将呈现模型记录(Ember提取的日历记录)。 model()
and I comment out this.store.findRecord
part out, the params are undefined and it raises an error. model()
记录参数 ,并且将this.store.findRecord
部分注释掉,则该参数是未定义的,并且会引发错误。 I thought at first that it is my DS.RESTAdapter since I have defined updateRecord changes to fake PUT request (my server does not allow that), but I commented out the whole file and it still does this query. 起初我以为是我的DS.RESTAdapter,因为我已将updateRecord更改定义为伪造的PUT请求(我的服务器不允许这样做),但是我注释掉了整个文件,但它仍然执行此查询。
How does Ember make POST request if model() hook is missing from route, I have no controllers difined. 如果路由中缺少model()钩子,我没有定义任何控制器,Ember怎么发出POST请求。 Also how do I fix it so that it works?
另外,我该如何解决它才能起作用? ;p
; p
Edit [2] : 编辑[2] :
I am trying this now and I think it kinda works, but looks ugly: 我现在正在尝试此操作,但我认为它有点用,但看起来很丑:
this.route('calendars',{ path: '/calendars'}, function(){
this.route('create');
});
this.route('calendar', { path: '/' }, function () {
this.route('show', { path: '/calendars/:calendar_id/show' });
this.route('edit', { path: '/calendars/:calendar_id/edit' });
});
this.route('index', { path: ''});
Ember is smart enough to generate a default route if you do not create one, and a default model if you do not create a model function. 如果您不创建默认路线,Ember足够聪明以生成默认路线;如果您不创建模型功能,则Ember可以生成默认模型。
It does this based on the routes name ie if your route is "calendar" it generates a model function based on the "calendar" model. 它基于路线名称执行此操作,即,如果您的路线是“日历”,则它将基于“日历”模型生成模型函数。
Try explicitly define your route path with the parameters as per ember docs: https://guides.emberjs.com/v2.9.0/routing/defining-your-routes/ 尝试按照ember docs的参数显式定义您的路线路径: https : //guides.emberjs.com/v2.9.0/routing/defining-your-routes/
this.route('calendar', function () {
this.route('show', { path: '/:calendar_id/show' });
this.route('edit', { path: '/:calendar_id/edit' });
this.route('create');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.