如何消除ember.js中的嵌套路由? [英] How do I disambiguate nested routes in ember.js?
问题描述
我有两个资源都具有相同的子资源:
I have two resources that both have the same sub-resource:
App.Router.map(function() {
this.resource('post', function() {
this.resource('comments', function() {
this.route('new');
});
});
this.resource('product', function() {
this.resource('comments', function() {
this.route('new');
});
});
});
问题是,Ember路由器只从当前和父级建立路由对象的名称路由,而不是整个层次结构。因此,它尝试路由 / posts /:id / comments / new
和 / products /:id / comments / new
到 App.NewCommentRoute
对象。我可以做些什么来解决这个问题?
The problem is that the ember router builds the names of the route objects out of just the current and parent routes, not out of the whole hierarchy. Thus, it tries to route both /posts/:id/comments/new
and /products/:id/comments/new
to the App.NewCommentRoute
object. What can I do to fix this?
这篇文章是从一个 GitHub问题。
推荐答案
我将James A. Rosen的解决方案进一步推进,魅力。有点多余,但让事情更加直观:
I took James A. Rosen's solution one step further and it worked like a charm. A bit redundant, but makes things much more intuitive down the road:
App.Router.map(function() {
this.resource('post', function() {
this.resource('post.comments', { path: '/comments' }, function() {
this.route('new');
});
});
this.resource('product', function() {
this.resource('product.comments', { path: '/comments' }, function() {
this.route('new');
});
});
});
现在,您可以使用 transitionTo('product.comments.new' )
或 App.register('route:product.comments.new',myRouteHandler)
正如预期的那样。
This now allows you to use transitionTo('product.comments.new')
or App.register('route:product.comments.new', myRouteHandler)
just as originally expected.
如果你不手动注册你的路由处理器,Ember,优雅地,甚至会在 App.ProductCommentsNewRoute
If you don't manually register your route handler, Ember, gracefully, will even look for it in App.ProductCommentsNewRoute
唯一的缺点是使用与父资源相同的根名称定义子资源的名称的冗余。
The only downside is the redundancy of defining the name of the sub-resource with the same root name that the parent resource already has.
这篇关于如何消除ember.js中的嵌套路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!