简体   繁体   English

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

[英]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应用程序的部分)时,问题开始了:

  • when I log params - they are undefined 当我记录参数时 -它们是未定义的
  • In dev tools I see that Ember somehow makes a POST request to my server as http://SERVER/calendars/5 (a :calendar_id part, SERVER is on same domain and where my back-end resides). 在开发工具中,我看到Ember以某种方式向我的服务器发出POST请求,即http:// SERVER / calendars / 5 (一个:calendar_id部分,SERVER在相同的域上,并且是我的后端所在)。
  • This happens regardless if I comment out model() function in app/routes/calendar/show.js file. 无论我是否注释掉app/routes/calendar/show.js文件中的model()函数,都会发生这种情况。
  • Apparently Ember knows what calendar_id to use for that request. 显然,Ember知道要用于该请求的calendar_id
  • But I don't know where that call to the server happens: 但是我不知道对服务器的调用发生在哪里:

    • If I comment out model(){} altogether, my template renders model record (the calendar record that Ember fetches). 如果我完全注释掉model(){} ,则我的模板将呈现模型记录(Ember提取的日历记录)。
    • If I on the other hand try to log params in 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请求(我的服务器不允许这样做),但是我注释掉了整个文件,但它仍然执行此查询。

  • I've cleaned both dist/ , tmp/ , upgraded to 2.9.0, but it does the same thing. 我已经清理了dist /tmp / ,都升级到了2.9.0,但是它做了同样的事情。
  • I have no controllers defined 我没有定义控制器

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM