fix(fetch): load sidebar and navbar for parent path, fixed #100

This commit is contained in:
qingwei.li 2017-02-28 21:16:10 +08:00 committed by cinwell.li
parent 07a2a91296
commit f3fc596951
2 changed files with 20 additions and 14 deletions

View File

@ -1,14 +1,23 @@
import { get } from './ajax'
import { callHook } from '../init/lifecycle'
import { getRoot } from '../route/util'
import { getParentPath } from '../route/util'
import { noop } from '../util/core'
function loadNested (path, file, next, vm, first) {
path = first ? path : path.replace(/\/$/, '')
path = getParentPath(path)
if (!path) return
get(vm.$getFile(path + file))
.then(next, _ => loadNested(path, file, next, vm))
}
export function fetchMixin (proto) {
let last
proto._fetch = function (cb = noop) {
const { path } = this.route
const { loadNavbar, loadSidebar } = this.config
const root = getRoot(path)
// Abort last request
last && last.abort && last.abort()
@ -26,25 +35,18 @@ export function fetchMixin (proto) {
const fn = result => { this._renderSidebar(result); cb() }
// Load sidebar
get(this.$getFile(root + loadSidebar))
// fallback root navbar when fail
.then(fn, _ => get(loadSidebar).then(fn))
loadNested(path, loadSidebar, fn, this, true)
},
_ => this._renderMain(null))
// Load nav
loadNavbar &&
get(this.$getFile(root + loadNavbar))
.then(
text => this._renderNav(text),
// fallback root navbar when fail
_ => get(loadNavbar).then(text => this._renderNav(text))
)
loadNested(path, loadNavbar, text => this._renderNav(text), this, true)
}
proto._fetchCover = function () {
const { coverpage } = this.config
const root = getRoot(this.route.path)
const root = getParentPath(this.route.path)
const path = this.$getFile(root + coverpage)
if (this.route.path !== '/' || !coverpage) {

View File

@ -45,8 +45,12 @@ export const isAbsolutePath = cached(path => {
return /(:|(\/{2}))/.test(path)
})
export const getRoot = cached(path => {
return /\/$/g.test(path) ? path : path.match(/(\S*\/)[^\/]+$/)[1]
export const getParentPath = cached(path => {
return /\/$/g.test(path)
? path
: (path = path.match(/(\S*\/)[^\/]+$/))
? path[1]
: ''
})
export const cleanPath = cached(path => {