如何在运行时向Ember.Router添加路由或状态?

cod*_*ger 2 javascript router ember.js ember-old-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
            })
        })
    })
});
Run Code Online (Sandbox Code Playgroud)

我希望能够做的是通过在声明它之后添加到App.Router来添加其他路由(这是为了启用任意模块).无论是在App.initialize()之前还是之后完成都不重要.

以下是模块路由对象的示例:

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

对此事的任何帮助都非常感谢.

Sté*_*ond 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();?
Run Code Online (Sandbox Code Playgroud)

编辑:我在GitHub上使用最新版本