【发布时间】:2013-02-08 08:08:08
【问题描述】:
有人可以解释为什么嵌套资源需要在路由名称中列出路径层次结构而不仅仅是路由吗?
例如。资源1 > 资源1.资源2
Emberjs 似乎就是为了减少代码量。是否有一些我看不到的资源用例解释了为什么应该以这种方式定义资源。
我无法让我的示例在 jsfiddle 或 jsbin 中工作,所以我将它托管在这里:http://emberjs.mattmazzola.net/
我的解决方案基于这个类似的 StackOverflow 问题中描述的技术:Ember.js pre4 multiple nested routing
基本上,您注意到我有一个资源“动物”,其中包含子资源“猫”和“狗”。但是,如果我分别将它们命名为“猫”和“狗”,路由器会说“找不到路由动物。猫”。然后如果我添加“动物”。前缀使嵌套路由'animals.cats'的url变成index#/animals/animals.cats这没有意义。当然我们通过覆盖路径属性来解决这个问题,但我不明白为什么Emberjs没有'默认情况下不这样做。我是否错误地定义了我的资源/路由,这是一个副作用?
换句话说,我目前正在这样做:
App.Router.map(function() {
this.resource('products', function() {
this.route('desktops');
this.route('laptops');
});
this.resource('animals', function() {
// the url for this route is bad, but default behavior?
this.resource('animals.cats', function() {
this.route('cat', {path: ':cat_id'});
});
// Why does this require stating the parent route 'animals' again?
this.resource('animals.dogs', {path: 'dogs/'}, function() {
this.route('dog', {path: ':dog_id'});
});
});
});
我怎样才能写出这样的路线:
App.Router.map(function() {
this.resource('products', function() {
this.route('desktops');
this.route('laptops');
});
this.resource('animals', function() {
this.resource('cats', function() {
this.route('cat', {path: ':cat_id'});
});
this.resource('dogs', function() {
this.route('dog', {path: ':dog_id'});
});
});
});
【问题讨论】:
标签: ember.js