fix dynamic routes

This commit is contained in:
Alexandre Chopin 2016-12-23 17:31:42 +01:00
parent 2fff5e7a4f
commit b33008ee88

View File

@ -198,15 +198,23 @@ function createRoutes (files, srcDir) {
let route = { name: '', path: '', component: r(srcDir, file), _name: null }
let parent = routes
keys.forEach((key, i) => {
route.name = route.name ? route.name + (key === 'index' ? '' : '-' + key.replace('_', '')) : key.replace('_', '')
route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '')
let child = _.find(parent, { name: route.name })
if (child) {
if (!child.children) {
child.children = []
}
parent = child.children
route.path = ''
} else {
route.path = route.path + (key === 'index' ? (i > 0 ? '' : '/') : '/' + key.replace('_', ':'))
if (key === 'index' && (i + 1) === keys.length) {
route.path += (i > 0 ? '' : '/')
} else {
route.path += '/' + key.replace('_', ':')
if (key.includes('_')) {
route.path += '?'
}
}
}
})
route._name = '_' + hash(route.component)
@ -222,15 +230,22 @@ function createRoutes (files, srcDir) {
}
function cleanChildrenRoutes (routes, isChild = false) {
let isOptional = true
let hasIndex = false
let parents = []
routes.forEach((route) => {
route.path = (isChild) ? route.path.replace('/', '') : route.path
if (route.path === '' || route.path === '/') {
isOptional = false
if ((isChild && /-index$/.test(route.name)) || (!isChild && route.name === 'index')) {
hasIndex = true
}
if (isOptional && route.path.includes(':')) {
route.path += '?'
route.path = (hasIndex) ? route.path.replace('?', '') : route.path
if (/-index$/.test(route.name)) {
parents.push(route.name)
} else {
if (parents.indexOf(route.name.split('-').slice(0, -1).join('-') + '-index') > -1) {
route.path = route.path.replace('?', '')
}
}
route.name = route.name.replace(/-index$/, '')
if (route.children) {
delete route.name
route.children = cleanChildrenRoutes(route.children, true)