【发布时间】:2016-10-26 14:59:32
【问题描述】:
对 ember 非常陌生,并尝试设置基本(在我看来)路线。
我有 calendars 资源,我想显示单个日历。
我的app/router.js 有以下内容:
this.route('calendar', {path: 'calendars/:calendar_id'}, function () {
this.route('show');
this.route('edit');
});
this.route('calendars', function(){
this.route('create');
});
文件夹如下:
app/routes: [
calendars: [create, index],
calendar: [edit, show]
]
app/templates: [
calendars: [create, index]
calendar: [edit, show]
]
在app/routes/calendar/show.js:
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
return this.store.findRecord('calendar', params.calendar_id);
}
});
当我转到http://SERVER/calendars/5/show 时问题就开始了(5 是 :calendar_id 部分,SERVER 是托管 ember 应用程序的部分):
- 当我记录 params - 它们是未定义的
- 在开发工具中,我看到 Ember 以某种方式向我的服务器发出 POST 请求,名称为 http://SERVER/calendars/5 (一个 :calendar_id 部分,SERVER 在同一个域上,并且我的后端所在的位置)。
- 无论我是否在
app/routes/calendar/show.js文件中注释掉model()函数,都会发生这种情况。 - 显然 Ember 知道该请求使用什么 calendar_id。
-
但我不知道对服务器的调用发生在哪里:
- 如果我完全注释掉
model(){},我的模板会呈现模型记录(Ember 获取的日历记录)。 - 另一方面,如果我尝试在
model()中记录 params 并注释掉this.store.findRecord部分,则 params 未定义并引发错误.
- 如果我完全注释掉
起初我以为它是我的 DS.RESTAdapter,因为我已将 updateRecord 更改定义为假 PUT 请求(我的服务器不允许这样做),但我注释掉了整个文件,它仍然执行此查询。
- 我已经清理了 dist/、tmp/,升级到了 2.9.0,但效果相同。
- 我没有定义控制器
如果路由中缺少 model() 钩子,Ember 如何发出 POST 请求,我没有定义控制器。另外我该如何修复它以使其正常工作? ;p
编辑[2]:
我现在正在尝试这个,我认为它有点工作,但看起来很难看:
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: ''});
【问题讨论】:
标签: javascript ember.js