refactor(core): add router

This commit is contained in:
qingwei.li 2017-02-18 11:41:33 +08:00 • committed by cinwell.li
commit 30da0d5d46
17 changed files with 316 additions and 88 deletions

View file

@ -1,13 +1,43 @@
import { get } from './ajax'
import { callHook } from '../init/lifecycle'
import { getCurrentRoot } from '../route/util'
export function fetchMixin (Docsify) {
Docsify.prototype.$fetch = function (path) {
// 加载侧边栏、导航、内容
let last
Docsify.prototype._fetch = function (cb) {
const { path } = this.route
const { loadNavbar, loadSidebar } = this.config
const currentRoot = getCurrentRoot(path)
// Abort last request
last && last.abort && last.abort()
last = get(this.$getFile(path), true)
last.then(text => {
this._renderMain(text)
if (!loadSidebar) return cb()
const fn = result => { this._renderSidebar(result); cb() }
// Load sidebar
get(this.$getFile(currentRoot + loadSidebar))
.then(fn, _ => get(loadSidebar).then(fn))
},
_ => this._renderMain(null))
// Load nav
loadNavbar &&
get(this.$getFile(currentRoot + loadNavbar))
.then(
this._renderNav,
_ => get(loadNavbar).then(this._renderNav)
)
}
}
export function initFetch (vm) {
vm.$fetch(result => {
vm._fetch(result => {
vm.$resetEvents()
callHook(vm, 'doneEach')
})