Skip to content

Call next's callback in beforeRouteUpdate #2054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add vm to beforeRouteUpdate's next callback
Fixes #1582
  • Loading branch information
thatguystone committed Feb 12, 2018
commit 097dca85c0d2f57b8d8c6888bfc47dd48549afdb
33 changes: 21 additions & 12 deletions src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ export class History {
activated
} = resolveQueue(this.current.matched, route.matched)

const postCbs = []
const isValid = () => this.current === route
const queue: Array<?NavigationGuard> = [].concat(
// in-component leave guards
extractLeaveGuards(deactivated),
// global before hooks
this.router.beforeHooks,
// in-component update hooks
extractUpdateHooks(updated),
extractUpdateHooks(updated, postCbs, isValid),
// in-config enter guards
activated.map(m => m.beforeEnter),
// async components
Expand Down Expand Up @@ -161,11 +163,9 @@ export class History {
}

runQueue(queue, iterator, () => {
const postEnterCbs = []
const isValid = () => this.current === route
// wait until async components are resolved before
// extracting in-component enter guards
const enterGuards = extractEnterGuards(activated, postEnterCbs, isValid)
const enterGuards = extractEnterGuards(activated, postCbs, isValid)
const queue = enterGuards.concat(this.router.resolveHooks)
runQueue(queue, iterator, () => {
if (this.pending !== route) {
Expand All @@ -175,7 +175,7 @@ export class History {
onComplete(route)
if (this.router.app) {
this.router.app.$nextTick(() => {
postEnterCbs.forEach(cb => { cb() })
postCbs.forEach(cb => { cb() })
})
}
})
Expand Down Expand Up @@ -266,10 +266,6 @@ function extractLeaveGuards (deactivated: Array<RouteRecord>): Array<?Function>
return extractGuards(deactivated, 'beforeRouteLeave', bindGuard, true)
}

function extractUpdateHooks (updated: Array<RouteRecord>): Array<?Function> {
return extractGuards(updated, 'beforeRouteUpdate', bindGuard)
}

function bindGuard (guard: NavigationGuard, instance: ?_Vue): ?NavigationGuard {
if (instance) {
return function boundRouteGuard () {
Expand All @@ -278,24 +274,37 @@ function bindGuard (guard: NavigationGuard, instance: ?_Vue): ?NavigationGuard {
}
}

function extractUpdateHooks (
updated: Array<RouteRecord>,
cbs: Array<Function>,
isValid: () => boolean
): Array<?Function> {
return extractGuards(updated, 'beforeRouteUpdate', (guard, instance, match, key) => {
var boundGuard = bindGuard(guard, instance)
if (boundGuard) {
return bindCbGuard(boundGuard, match, key, cbs, isValid)
}
})
}

function extractEnterGuards (
activated: Array<RouteRecord>,
cbs: Array<Function>,
isValid: () => boolean
): Array<?Function> {
return extractGuards(activated, 'beforeRouteEnter', (guard, _, match, key) => {
return bindEnterGuard(guard, match, key, cbs, isValid)
return bindCbGuard(guard, match, key, cbs, isValid)
})
}

function bindEnterGuard (
function bindCbGuard (
guard: NavigationGuard,
match: RouteRecord,
key: string,
cbs: Array<Function>,
isValid: () => boolean
): NavigationGuard {
return function routeEnterGuard (to, from, next) {
return function (to, from, next) {
return guard(to, from, cb => {
next(cb)
if (typeof cb === 'function') {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy