为什么嵌套资源路由重置Ember中的命名空间? [英] Why do nested resource routes reset the namespace in Ember?

查看:114
本文介绍了为什么嵌套资源路由重置Ember中的命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个照片模型和一个 Post 模型,我希望这两个都有注释的。在Rails中,我的路线将如下所示:

  Rails.application.routes.draw $ $ $ $ $ $ $ $ $ $ $ $ $ $资源:帖子,只有:[:show] do 
资源:注释,只有:[:index]
end

资源:照片,只有:[:show] b $ b资源:注释,只有:[: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 ,我会使用 photo_comments(9)



如果我想在Ember中创建相同的路线,我会做:

  App.Router.map() - > 

@resource'posts', - >
@resource'post',{path:'/:post_id'}, - >
@resource'comments'

@resource'照片', - >
@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_id / comments / photos /:photo_id / comments ,这是我想要的。但是,因为Ember重置命名空间,所以我不再有 post_comments photo_comments helpers。我有一个评论路由,路由到 / photos /:photo_id / comments ,但我没有路由到 / posts /:posts_id / comments 的方式。我意识到我可以通过执行以下操作来解决这个问题,但似乎是多余的:

  App.Router.map() - > 

@resource'posts', - >
@resource'post',{path:'/:post_id'}, - >
@resource'posts.comments',{path:'/ comments'}

@resource'照片', - >
@resource'photo',{path:'/:photo_id'}, - >
@resource'photos.comments',{path:'/ comments'}

TL / DR:



我明白 Ember重置嵌套资源的路由,但我不明白>为什么。可以有人向我解释吗?

解决方案

TL; DR资源必须是唯一的,因为过渡范式,只是覆盖了评论资源。



这是因为当您转换到路由时,不会显式调出整个路径。

  this.transitionTo('照片',照片); 

{{#link-to'photo'photo}}我的照片{{/ link-to}}

因为这个资源必须是唯一的。



如果你在你的应用程序的根目录,想要跳2级深度,一张照片你只需要使用 this.transitionTo('照片',照片),并且使用 this.transitionTo('photos.photo',照片) / p>

如果您正在转换为多个动态资源的资源,您只需在多个模型中发送。 this.transitionTo('foo',bar,baz)



如所暗示的,您可以迫使人们在进行转换/链接时指定整个路径,但作者决定惩罚较小百分比的重复资源的人另外据了解, foo.bar 代表 resource.route 在Ember中。我不会认为这是一个争论,为什么它是如此的架构,更多的是关于它的陈述。

  @resource 'posts', - > 
@resource'post',{path:'/:post_id'}
@route'foo'

this.transitionTo('posts.foo');


Let's say I have a Photo model and a Post model, and I want both of those to have Comment's. In Rails, my routes would look like this:

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

This generates the following routes:

GET /posts/:post_id/comments(.:format)
GET /posts/:id(.:format)
GET /photos/:photo_id/comments(.:format)
GET /photos/:id(.:format)

Okay, makes sense. If I want to get the path to the Comment's for the Photo with an ID of 9, I'd use photo_comments(9).

If I wanted to create the same routes in Ember, I'd do:

App.Router.map () ->

  @resource 'posts', ->
    @resource 'post', { path: '/:post_id' }, ->
      @resource 'comments'

  @resource 'photos', ->
    @resource 'photo', { path: '/:photo_id' }, ->
      @resource 'comments'

In Ember, this generates the following URLs:

#/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

I still have /posts/:posts_id/comments and /photos/:photo_id/comments, which is what I wanted. However, because Ember resets the namespace, I no longer have post_comments and photo_comments helpers. I have a comments route, which routes to /photos/:photo_id/comments, but I don't have any way of routing to /posts/:posts_id/comments. I realize I could fix this by doing the following, but it seems redundant:

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:

I understand that Ember resets routes for nested resources, but I don't understand why. Could somebody explain it to me?

解决方案

TL;DR Resources must be unique due to the transitioning paradigm, and really you're just over-writing the comments resource.

It's because when you transition to routes you don't explicitly call out an entire path.

this.transitionTo('photo', photo);

{{#link-to 'photo' photo}} My photo{{/link-to}}

Because of this resources must be unique.

If you were at the root of your app and wanted to jump 2 levels deep, to a photo you would just use this.transitionTo('photo', photo), and would NOT use this.transitionTo('photos.photo', photo)

If you are transitioning to a resource that is multiple dynamic resources deep you just send in multiple models. this.transitionTo('foo', bar, baz).

As was implied, you could force people to state an entire path while doing transitionTo/link-to, but the authors decided to punish the smaller percent of people with duplicate resources vs punish everyone into defining the entire path while transitioning.

Additionally it's understood that foo.bar represents resource.route in Ember. I wouldn't consider this an argument for why it was architected as it was, more of a statement about it.

@resource 'posts', ->
   @resource 'post', { path: '/:post_id' }
   @route 'foo'

this.transitionTo('posts.foo');

这篇关于为什么嵌套资源路由重置Ember中的命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆