Ember.js 为什么嵌套资源路由会重置Ember中的命名空间?
假设我有一个Ember.js 为什么嵌套资源路由会重置Ember中的命名空间?,ember.js,Ember.js,假设我有一个照片模型和一个帖子模型,我希望这两个模型都有评论。在Rails中,我的路线如下所示: Rails.application.routes.draw do resources :posts, only: [ :show ] do resources :comments, only: [ :index ] end resources :photos, only: [ :show ] do resources :comments, only: [ :index
照片
模型和一个帖子
模型,我希望这两个模型都有评论
。在Rails中,我的路线如下所示:
Rails.application.routes.draw do
resources :posts, only: [ :show ] do
resources :comments, only: [ :index ]
end
resources :photos, only: [ :show ] do
resources :comments, only: [ :index ]
end
end
这将生成以下路由:
GET /posts/:post_id/comments(.:format)
GET /posts/:id(.:format)
GET /photos/:photo_id/comments(.:format)
GET /photos/:id(.:format)
好的,有道理。如果我想获得ID为9
的照片的注释的路径,我会使用照片注释(9)
如果我想在Ember中创建相同的路线,我会:
App.Router.map () ->
@resource 'posts', ->
@resource 'post', { path: '/:post_id' }, ->
@resource 'comments'
@resource 'photos', ->
@resource 'photo', { path: '/:photo_id' }, ->
@resource 'comments'
在Ember中,这将生成以下URL:
#/loading
#/posts/loading
#/posts/:post_id/loading
#/posts/:post_id
#/posts
#/photos/:photo_id/comments
#/photos/:photo_id/loading
#/photos/:photo_id
#/photos/loading
#/photos
#/
#/photos/:photo_id/loading
我仍然有/posts/:posts\u id/comments
和/photos/:photo\u id/comments
,这正是我想要的。但是,由于Ember重置了名称空间,因此我不再有post_注释
和photo_注释
助手。我有一个comments
路由,路由到/photos/:photo\u id/comments
,但我没有任何路由到/posts/:posts\u id/comments
。我意识到我可以通过执行以下操作来解决此问题,但这似乎是多余的:
App.Router.map () ->
@resource 'posts', ->
@resource 'post', { path: '/:post_id' }, ->
@resource 'posts.comments', { path: '/comments' }
@resource 'photos', ->
@resource 'photo', { path: '/:photo_id' }, ->
@resource 'photos.comments', { path: '/comments' }
TL/DR:
我知道Ember会重置嵌套资源的路由,但我不明白为什么。谁能给我解释一下吗;由于过渡范式的原因,DR资源必须是唯一的,实际上您只是在过度编写注释资源
这是因为当您转换到路由时,不会显式调用整个路径
this.transitionTo('photo', photo);
{{#link-to 'photo' photo}} My photo{{/link-to}}
因此,资源必须是唯一的
如果你是应用程序的根用户,想跳转2级,到一张照片,你只需使用this.transition('photo',photo)
,而不使用this.transition('photos.photo',photo)
如果您要转换到一个包含多个动态资源的资源,您只需发送多个模型<代码>此。转换为('foo',bar,baz)
正如所暗示的,您可以强制人们在执行转换/链接到时声明整个路径,但作者决定惩罚具有重复资源的人的比例要比惩罚所有人在转换时定义整个路径的比例低
此外,可以理解,foo.bar
表示余烬中的resource.route
。我不认为这是为什么它是架构的争论,更多的是关于它的声明。< /P>
@resource 'posts', ->
@resource 'post', { path: '/:post_id' }
@route 'foo'
this.transitionTo('posts.foo');
这是有道理的!谢谢你的及时和详细的回答。我已经发布了一个。你介意看一下吗?