EmberJS 2路由动态段未嵌套路由未定义

时间:2016-10-26 14:59:32

标签: javascript ember.js

非常陌生,试图设置基本(在我的脑海中)路线。

我有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应用程序):

  • 当我记录参数时 - 它们未定义
  • 在开发工具中,我看到Ember以某种方式向我的服务器发出POST请求http://SERVER/calendars/5 (a :calendar_id 部分,SERVER位于同一个域,我的后端所在的位置)。
  • 如果我在model()文件中注释app/routes/calendar/show.js函数,则会发生这种情况。
  • 显然Ember知道要用于该请求的 calendar_id
  • 但我不知道对服务器的调用发生在哪里:

    • 如果我完全注释掉model(){},我的模板会呈现模型记录(Ember提取的日历记录)。
    • 如果我另一方面尝试在model()中记录参数而我将this.store.findRecord注释掉,那么参数是未定义的它引发了一个错误。
  • 我起初认为它是我的DS.RESTAdapter,因为我已经定义了对假PUT请求的updateRecord更改(我的服务器不允许),但我注释掉了整个文件,它仍然执行此查询。

  • 我已经清除 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: ''});

1 个答案:

答案 0 :(得分:1)

如果不创建默认路由,Ember足够智能生成默认路由,如果不创建模型函数,则生成默认模型。

它基于路线名称来实现,即如果您的路线是"日历"它根据"日历"生成模型功能。模型。

尝试使用ember文档中的参数显式定义路径路径: 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');
});
相关问题