Why do nested resource routes reset the namespace in Ember?

倖福魔咒の 提交于 2019-12-20 02:49:12

问题


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?


回答1:


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');


来源:https://stackoverflow.com/questions/23946960/why-do-nested-resource-routes-reset-the-namespace-in-ember

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!