Files
antd-admin/src/router.js
T
2017-04-01 16:46:32 +08:00

144 lines
4.2 KiB
JavaScript

import React, { PropTypes } from 'react'
import { Router, routerRedux } from 'dva/router'
import App from './routes/app'
const cached = {}
const registerModel = (app, model) => {
if (!cached[model.namespace]) {
app.model(model)
cached[model.namespace] = 1
}
}
const Routers = function ({ history, app }) {
const handleChildRoute = ({ location }) => {
const state = app._store.getState()
if (!state.app.login && location.pathname !== '/login') {
app._store.dispatch(routerRedux.push('/login'))
}
}
const routes = [
{
path: '/',
component: App,
getIndexRoute (nextState, cb) {
require.ensure([], require => {
registerModel(app, require('./models/dashboard'))
cb(null, { component: require('./routes/dashboard/') })
}, 'dashboard')
},
childRoutes: [
{
path: 'dashboard',
getComponent (nextState, cb) {
require.ensure([], require => {
registerModel(app, require('./models/dashboard'))
cb(null, require('./routes/dashboard/'))
}, 'dashboard')
},
}, {
path: 'users',
getComponent (nextState, cb) {
require.ensure([], require => {
registerModel(app, require('./models/users'))
cb(null, require('./routes/users/'))
}, 'users')
},
}, {
path: 'request',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/request/'))
}, 'request')
},
}, {
path: 'UIElement/iconfont',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/UIElement/iconfont/'))
}, 'UIElement-iconfont')
},
}, {
path: 'UIElement/search',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/UIElement/search/'))
}, 'UIElement-search')
},
}, {
path: 'UIElement/dropOption',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/UIElement/dropOption/'))
}, 'UIElement-dropOption')
},
}, {
path: 'UIElement/layer',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/UIElement/layer/'))
}, 'UIElement-layer')
},
}, {
path: 'UIElement/dataTable',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/UIElement/dataTable/'))
}, 'UIElement-dataTable')
},
}, {
path: 'UIElement/editor',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/UIElement/editor/'))
}, 'UIElement-editor')
},
}, {
path: 'chart/lineChart',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/chart/lineChart/'))
}, 'chart-lineChart')
},
}, {
path: 'chart/barChart',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/chart/barChart/'))
}, 'chart-barChart')
},
}, {
path: 'chart/areaChart',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/chart/areaChart/'))
}, 'chart-areaChart')
},
}, {
path: '*',
getComponent (nextState, cb) {
require.ensure([], require => {
cb(null, require('./routes/error/'))
}, 'error')
},
},
],
},
]
routes[0].childRoutes.map(item => {
item.onEnter = handleChildRoute
return item
})
return <Router history={history} routes={routes} />
}
Routers.propTypes = {
history: PropTypes.object,
app: PropTypes.object,
}
export default Routers