[英]Emberjs pre4 nested route default URI
有人可以解釋為什么嵌套資源需要在路由名稱中而不是僅在路由名稱中列出路徑層次結構嗎?
例如。 resource1> resource1.resource2
Emberjs似乎與減少代碼量有關。 我沒有看到一些資源用例,這解釋了為什么應該用這種方式定義資源。
我無法在jsfiddle或jsbin中使用示例,因此將其托管在這里: http ://emberjs.mattmazzola.net/
我的解決方案基於類似StackOverflow問題中所述的技術: Ember.js pre4多重嵌套路由
基本上,您注意到我有一個帶有子資源“貓”和“狗”的資源“動物”。 但是,如果我分別將它們分別命名為“ cats”和“ dogs”,則路由器會說“找不到route animals.cats”。然后添加“ animals”。 將嵌套路由設置為“ animals.cats”的前綴,URL變為index#/ animals / animals.cats,這毫無意義。我們當然會通過覆蓋path屬性來解決此問題,但我不明白為什么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'});
});
});
});
嗯,我認為如果您正確定義了App.AnimalsIndexRoute
, App.CatsIndexRoute
和App.DogsIndexRoute
(以及其他一些Ember.Routes),第二個版本應該可以工作。 如果仍有問題,可以將其余的代碼發布在這里還是jsfiddle中?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.