Jam*_*sen 6 javascript ember.js ember-router
我有两个资源都具有相同的子资源:
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');
});
});
});
Run Code Online (Sandbox Code Playgroud)
问题是,ember路由器只根据当前和父路由构建路由对象的名称,而不是整个层次结构.因此,它会尝试路线都/posts/:id/comments/new和/products/:id/comments/new到App.NewCommentRoute对象.我该怎么做才能解决这个问题?
这篇文章改编自GitHub问题.
我把James A. Rosen的解决方案更进了一步,它就像一个魅力.有点多余,但让事情更加直观:
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');
});
});
});
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用transitionTo('product.comments.new')或App.register('route:product.comments.new', myRouteHandler)仅按原先的预期使用.
如果你没有手动注册你的路由处理程序,Ember,优雅地,甚至会查找它 App.ProductCommentsNewRoute
唯一的缺点是使用与父资源已有的相同的根名称来定义子资源名称的冗余.
| 归档时间: |
|
| 查看次数: |
907 次 |
| 最近记录: |