命名空间路由的目录结构是什么?

时间:2015-01-14 12:45:44

标签: ember.js ember-data ember-cli

我创建了一个名为namespaced的{​​{1}}路线。

我在checkout中有以下内容。

router.js

可通过以下方式访问以下路线:

// router.js
this.route('checkout', function() {
  this.resource('payments', function() {
  });
});

但是,http://localhost/checkout/payments 的控制器,模板,路由不存在于payment目录中。我会想象:

checkout

但相反它们存在并且可以被Ember访问:

app/controllers/checkout/payments/index.js
app/routes/checkout/payments/index.js
app/templates/checkout/payments/index.hbs

这是预期的吗?

2 个答案:

答案 0 :(得分:1)

我认为你不能在routes内嵌套routes你确定你不是在谈论resources吗?

A resource always resets the namespace.

  

评论资源的路由,控制器和视图类名   没有Post的前缀。 资源始终重置命名空间,   确保可以在多个父级之间重用类   资源和类名不会越长越深嵌套   资源是。

强调我的

答案 1 :(得分:1)

它的工作原理部分归功于Asgaroth描述的内容:资源重置名称空间,因此在您的示例中,ember-cli正在寻找类Payments而不是CheckoutPayments,部分原因是由于ember -cli支持两个目录布局(常规布局和" pod"布局)。在构建应用程序时,它会从文件目录中获取类的名称,具体取决于哪个类。

您基本上将这两种方法混合并匹配到文件系统布局,这就是它可以工作的原因。但如果没有更好的结构,找出属于什么的东西将是非常困难的。

相反,我使用ember generate --pod ...结构(应该推荐使用Ember 2.0),这导致我的文件结构与您的问题有些相似:

app/map
├── debug
│   ├── controller.js
│   ├── route.js
│   └── template.hbs
├── index
│   ├── controller.js
│   └── template.hbs
├── settings
│   ├── controller.js
│   └── template.hbs
├── systems
│   ├── system
│   │   ├── route.js
│   │   └── template.hbs
│   ├── controller.js
│   ├── route.js
│   └── template.hbs
├── controller.js
└── template.hbs

是的,你可以嵌套路由(自Ember 1.7左右)和ES6模块,它们对路由使用资源几乎没有好处。我已经停止使用它们了,根据我router.js上方的结构现在是相当无足轻重的:

this.route( 'map', function() {
  this.route( 'systems', function() {
    this.route( 'system', { path: ':system_id' } );
  });
  this.route( 'settings' );
  this.route( 'debug' );
});

我强烈推荐你的路由器,控制器,mixin和组件的pod结构。虽然需要一些人习惯,你的编辑器必须显示目录名,才能知道你在编辑什么。