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

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

问题描述

假设我有一个 Photo 模型和一个 Post 模型,我希望这两个模型都有 Comment 的.在 Rails 中,我的路线如下所示:

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

这会生成以下路由:

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

好的,有道理.如果我想获取 ID 为 9PhotoComment 的路径,我会使用 photo_comments(9).

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).

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

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'

在 Ember 中,这会生成以下 URL:

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

我还有 /posts/:posts_id/comments/photos/:photo_id/comments,这正是我想要的.然而,因为 Ember 重置了命名空间,我不再有 post_commentsphoto_comments 助手.我有一个 comments 路由,它路由到 /photos/:photo_id/comments,但我没有任何路由到 /posts/:posts_id 的方法/评论.我意识到我可以通过执行以下操作来解决此问题,但这似乎是多余的:

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:

我理解 Ember 重置嵌套资源的路由,但我不明白为什么.有人可以给我解释一下吗?

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

推荐答案

TL;DR 资源必须是唯一的,因为过渡范式,而实际上您只是覆盖了评论资源.

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.

如果你在你的应用程序的根目录下并且想要跳到 2 级深度,你可以使用 this.transitionTo('photo', photo),并且 使用 this.transitionTo('photos.photo', photo)

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)

如果您要过渡到包含多个动态资源深度的资源,您只需发送多个模型即可.this.transitionTo('foo', bar, baz).

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

正如暗示的那样,您可以强制人们在执行 transitionTo/link-to 时说明整个路径,但作者决定惩罚少数拥有重复资源的人,而不是惩罚每个人在转换时定义整个路径.

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.

另外据了解,foo.bar 在 Ember 中代表 resource.route.我不会认为这是一个关于为什么它被这样设计的论据,更多的是关于它的声明.

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天全站免登陆