fix(fetch): load sidebar and navbar for parent path, fixed #100
This commit is contained in:
parent
07a2a91296
commit
f3fc596951
2 changed files with 20 additions and 14 deletions
|
|
@ -1,14 +1,23 @@
|
||||||
import { get } from './ajax'
|
import { get } from './ajax'
|
||||||
import { callHook } from '../init/lifecycle'
|
import { callHook } from '../init/lifecycle'
|
||||||
import { getRoot } from '../route/util'
|
import { getParentPath } from '../route/util'
|
||||||
import { noop } from '../util/core'
|
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) {
|
export function fetchMixin (proto) {
|
||||||
let last
|
let last
|
||||||
proto._fetch = function (cb = noop) {
|
proto._fetch = function (cb = noop) {
|
||||||
const { path } = this.route
|
const { path } = this.route
|
||||||
const { loadNavbar, loadSidebar } = this.config
|
const { loadNavbar, loadSidebar } = this.config
|
||||||
const root = getRoot(path)
|
|
||||||
|
|
||||||
// Abort last request
|
// Abort last request
|
||||||
last && last.abort && last.abort()
|
last && last.abort && last.abort()
|
||||||
|
|
@ -26,25 +35,18 @@ export function fetchMixin (proto) {
|
||||||
const fn = result => { this._renderSidebar(result); cb() }
|
const fn = result => { this._renderSidebar(result); cb() }
|
||||||
|
|
||||||
// Load sidebar
|
// Load sidebar
|
||||||
get(this.$getFile(root + loadSidebar))
|
loadNested(path, loadSidebar, fn, this, true)
|
||||||
// fallback root navbar when fail
|
|
||||||
.then(fn, _ => get(loadSidebar).then(fn))
|
|
||||||
},
|
},
|
||||||
_ => this._renderMain(null))
|
_ => this._renderMain(null))
|
||||||
|
|
||||||
// Load nav
|
// Load nav
|
||||||
loadNavbar &&
|
loadNavbar &&
|
||||||
get(this.$getFile(root + loadNavbar))
|
loadNested(path, loadNavbar, text => this._renderNav(text), this, true)
|
||||||
.then(
|
|
||||||
text => this._renderNav(text),
|
|
||||||
// fallback root navbar when fail
|
|
||||||
_ => get(loadNavbar).then(text => this._renderNav(text))
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
proto._fetchCover = function () {
|
proto._fetchCover = function () {
|
||||||
const { coverpage } = this.config
|
const { coverpage } = this.config
|
||||||
const root = getRoot(this.route.path)
|
const root = getParentPath(this.route.path)
|
||||||
const path = this.$getFile(root + coverpage)
|
const path = this.$getFile(root + coverpage)
|
||||||
|
|
||||||
if (this.route.path !== '/' || !coverpage) {
|
if (this.route.path !== '/' || !coverpage) {
|
||||||
|
|
|
||||||
|
|
@ -45,8 +45,12 @@ export const isAbsolutePath = cached(path => {
|
||||||
return /(:|(\/{2}))/.test(path)
|
return /(:|(\/{2}))/.test(path)
|
||||||
})
|
})
|
||||||
|
|
||||||
export const getRoot = cached(path => {
|
export const getParentPath = cached(path => {
|
||||||
return /\/$/g.test(path) ? path : path.match(/(\S*\/)[^\/]+$/)[1]
|
return /\/$/g.test(path)
|
||||||
|
? path
|
||||||
|
: (path = path.match(/(\S*\/)[^\/]+$/))
|
||||||
|
? path[1]
|
||||||
|
: ''
|
||||||
})
|
})
|
||||||
|
|
||||||
export const cleanPath = cached(path => {
|
export const cleanPath = cached(path => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue