2012-06-07 38 views
2

我有我的應用程序如下Ember.Router:如何在運行時向Ember.Router添加路由或狀態?

App.Router = Ember.Router.extend({ 
    location: 'hash', 
    rootElement: '#content', 
    enableLogging: true, 

    root: Ember.State.extend({ 
     route: '/', 

     index: Ember.State.extend({ 
      route: '/', 
      redirectsTo: 'main.welcome' 
     }), 

     main: Ember.State.extend({ 
      route: '/main', 

      welcome: Ember.ViewState.extend({ 
       route: '/welcome', 
       view: App.WelcomeView 
      }) 
     }) 
    }) 
}); 

我希望能夠做的是它已經被宣佈後增加了App.Router添加額外的路由(這是使任意模塊)。無論是在App.initialize()之前還是之後都不重要。

這裏是關於模塊路由對象會是什麼樣子的例子:

Module.routes = Ember.State.extend({ 
    route: '/module', 
    index: Ember.State.extend({ 
     route: '/' 
     view: Module.IndexView 
    }) 
}); 

對此事的任何幫助深表感謝。

回答

5

您可以提取想要豐富的狀態,以便稍後重新打開。

App = Ember.Application.create(); 

App.RootState = Em.State.extend({ 
    index : Em.State.extend({ 
     route : '/' 
    }), 
    main: Em.State.extend({ 
     route : '/main', 
     index : Em.State.extend({ 
      route : '/' 
     }) 
    }) 
}); 

App.Router = Ember.Router.extend({ 
    location : 'hash', 
    enableLogging : true, 
    root : App.RootState 
}); 

// later... 

App.RootState.reopen({ 
    module: Em.State.extend({ 
     route : '/module', 
     index : Em.State.extend({ 
      route : '/' 
     }) 
    }) 
}); 

App.initialize();​ 

編輯:我使用的最新版本在GitHub上

+0

謝謝你,解決了我的問題! :d – codehugger