2013-11-14 209 views
4

重用的资源,我有以下路由设置:灰烬路由:在多个父路径

this.resource('blog',function(){ 
    this.resource('selectimage',{path:"selectimage/:returncontext"},function(){}); 
}); 

this.resource('post',{path:":post_id"},function(){ 
    this.resource('selectimage',{path:"selectimage/:returncontext"},function(){}); 
}); 

我本来期望是,导航到blog.selectimage当post.selectimage,即同一个路由器,控制器和视图将被使用。 不幸的是,似乎最后一个条目赢了。

是否有可能实现这一点,而不离开父上下文(后/博客),当导航到selectimage (很明显,我可以继承base-selectimages类,但我希望有一些通用的烬)

回答

5

对于两条不同的路由,你不能拥有相同的资源名称,因为Ember更多地依赖于命名约定。但是灰烬提供了一些方法来重复使用相同的控制器和模板

下重命名后您的selectimage到别的

this.resource('post',{path:":post_id"},function(){ 
    this.resource('xxx',{path:"xxx/:returncontext"}); 
}); 

的东西,在路由器中,你可以指定templateNamecontroller有被重用。这样的事情应该使其可重复使用

App.xxxRoute = Em.Route.extend({ 
    controllerName: 'selectimage', 
    templateName: 'selectimage' 
}); 

样品Demo

+0

尼斯。我不知道你可以在Route中指定控制器和模板。 – AyKarsi

+0

..刚刚检查了文档@ http://emberjs.com/api/classes/Ember.Route.html:没有提及controllerName和templateName。看看源代码中的渲染方法,我发现你也可以指定viewName。 – AyKarsi

+0

幸福,帮助你:) Ember Docs越来越好。相信它会更快 – selvagsz