2013-02-25 33 views
8

我正在使用Ember.js,並且我想創建一個捕獲所有路由,以便將用戶返回到應用程序的根目錄,如果它們導航到的URL不匹配資源。 (我用的是歷史API)我已經實現了這個像這樣:在ember.js中定義多段式捕獲所有路由

App.Router.map(function() { 
    this.resource('things', function() { 
     this.resource('thing', {path:':thing_id'}); 
    }); 
    this.route('catchAll', { path: ':*' }); 
    this.route('catchAll', { path: ':*/:*' }); 
    this.route('catchAll', { path: ':*/:*/:*' }); 
}); 

App.Router.reopen({ 
    location: 'history' 
}); 

App.CatchAllRoute = Ember.Route.extend({ 
    redirect: function() { 
     this.transitionTo('index'); 
    } 
}); 

App.IndexRoute = Ember.Route.extend({ 

}); 

我的問題是:我可以定義一個包羅萬象的路線,將符合尚未解決的資源,不論數量的任何路徑段中的路徑?

我使用Ember.VERSION:1.0.0-RC.1

回答

21

一些擺弄我想我已經找到了解決辦法之後:*:似乎這樣的伎倆,像

this.route('catchAll', { path: '*:' }); 

我設置了this fiddle來演示它是如何工作的。

+0

Ups,發現複製粘貼錯誤,代碼已更新。 – 2013-02-27 09:05:41

+0

完美!謝謝! – i0n 2013-02-27 11:28:33

+1

這是正確的,或多或少,但有點混亂。 ':'是你給這個捕獲所有部分的名字。而不是'* wildcardurl'或類似的。就像你在模型設置中用參數做事一樣。 – 2014-02-20 13:49:25