ember.js - Why do nested resource routes reset the namespace in Ember? -
let's have photo
model , post
model, , want both of have comment
's. in rails, routes this:
rails.application.routes.draw resources :posts, only: [ :show ] resources :comments, only: [ :index ] end resources :photos, only: [ :show ] resources :comments, only: [ :index ] end end
this generates following routes:
get /posts/:post_id/comments(.:format) /posts/:id(.:format) /photos/:photo_id/comments(.:format) /photos/:id(.:format)
okay, makes sense. if want path comment
's photo
id of 9
, i'd use photo_comments(9)
.
if wanted create 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, generates 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
, /photos/:photo_id/comments
, wanted. however, because ember resets namespace, no longer have post_comments
, photo_comments
helpers. have comments
route, routes /photos/:photo_id/comments
, don't have way of routing /posts/:posts_id/comments
. realize fix doing following, 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 nested resources, don't understand why. explain me?
tl;dr resources must unique due transitioning paradigm, , you're over-writing comments resource.
it's because when transition routes don't explicitly call out entire path.
this.transitionto('photo', photo); {{#link-to 'photo' photo}} photo{{/link-to}}
because of resources must unique.
if @ root of app , wanted jump 2 levels deep, photo use this.transitionto('photo', photo)
, , not use this.transitionto('photos.photo', photo)
if transitioning resource multiple dynamic resources deep send in multiple models. this.transitionto('foo', bar, baz)
.
as implied, force people state entire path while doing transitionto/link-to, authors decided punish smaller percent of people duplicate resources vs punish defining entire path while transitioning.
additionally it's understood foo.bar
represents resource.route
in ember. wouldn't consider argument why architected was, more of statement it.
@resource 'posts', -> @resource 'post', { path: '/:post_id' } @route 'foo' this.transitionto('posts.foo');
Comments
Post a Comment