Skip to content

Commit fd0224a

Browse files
committed
feat: support onComplete and onAbort callbacks for router.push and router.replace
1 parent 732010d commit fd0224a

File tree

6 files changed

+94
-18
lines changed

6 files changed

+94
-18
lines changed

Diff for: src/history/abstract.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ export class AbstractHistory extends History {
1313
this.index = -1
1414
}
1515

16-
push (location: RawLocation) {
16+
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
1717
this.transitionTo(location, route => {
1818
this.stack = this.stack.slice(0, this.index + 1).concat(route)
1919
this.index++
20-
})
20+
onComplete && onComplete(route)
21+
}, onAbort)
2122
}
2223

23-
replace (location: RawLocation) {
24+
replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
2425
this.transitionTo(location, route => {
2526
this.stack = this.stack.slice(0, this.index).concat(route)
26-
})
27+
onComplete && onComplete(route)
28+
}, onAbort)
2729
}
2830

2931
go (n: number) {

Diff for: src/history/hash.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ export class HashHistory extends History {
3434
})
3535
}
3636

37-
push (location: RawLocation) {
37+
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
3838
this.transitionTo(location, route => {
3939
pushHash(route.fullPath)
40-
})
40+
onComplete && onComplete(route)
41+
}, onAbort)
4142
}
4243

43-
replace (location: RawLocation) {
44+
replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
4445
this.transitionTo(location, route => {
4546
replaceHash(route.fullPath)
46-
})
47+
onComplete && onComplete(route)
48+
}, onAbort)
4749
}
4850

4951
go (n: number) {

Diff for: src/history/html5.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,22 @@ export class HTML5History extends History {
4747
window.history.go(n)
4848
}
4949

50-
push (location: RawLocation) {
50+
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
5151
const current = this.current
5252
this.transitionTo(location, route => {
5353
pushState(cleanPath(this.base + route.fullPath))
5454
this.handleScroll(route, current, false)
55-
})
55+
onComplete && onComplete(route)
56+
}, onAbort)
5657
}
5758

58-
replace (location: RawLocation) {
59+
replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
5960
const current = this.current
6061
this.transitionTo(location, route => {
6162
replaceState(cleanPath(this.base + route.fullPath))
6263
this.handleScroll(route, current, false)
63-
})
64+
onComplete && onComplete(route)
65+
}, onAbort)
6466
}
6567

6668
ensureURL (push?: boolean) {

Diff for: src/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ export default class VueRouter {
101101
this.afterHooks.push(fn)
102102
}
103103

104-
push (location: RawLocation) {
105-
this.history.push(location)
104+
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
105+
this.history.push(location, onComplete, onAbort)
106106
}
107107

108-
replace (location: RawLocation) {
109-
this.history.replace(location)
108+
replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
109+
this.history.replace(location, onComplete, onAbort)
110110
}
111111

112112
go (n: number) {

Diff for: test/unit/specs/navigation-callback.spec.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import Router from '../../../src/index'
2+
3+
fdescribe('Programmatic navigation callbacks', () => {
4+
let calls = []
5+
let router, spy1, spy2
6+
7+
const Foo = {
8+
beforeRouteEnter (to, from, next) {
9+
calls.push(3)
10+
setTimeout(() => {
11+
calls.push(4)
12+
next()
13+
}, 1)
14+
}
15+
}
16+
17+
beforeEach(() => {
18+
calls = []
19+
spy1 = jasmine.createSpy('complete')
20+
spy2 = jasmine.createSpy('abort')
21+
22+
router = new Router({
23+
routes: [
24+
{ path: '/foo', component: Foo }
25+
]
26+
})
27+
28+
router.beforeEach((to, from, next) => {
29+
calls.push(1)
30+
setTimeout(() => {
31+
calls.push(2)
32+
next()
33+
}, 1)
34+
})
35+
})
36+
37+
it('push complete', done => {
38+
router.push('/foo', () => {
39+
expect(calls).toEqual([1, 2, 3, 4])
40+
done()
41+
})
42+
})
43+
44+
it('push abort', done => {
45+
router.push('/foo', spy1, spy2)
46+
router.push('/bar', () => {
47+
expect(calls).toEqual([1, 1, 2, 2])
48+
expect(spy1).not.toHaveBeenCalled()
49+
expect(spy2).toHaveBeenCalled()
50+
done()
51+
})
52+
})
53+
54+
it('replace complete', done => {
55+
router.replace('/foo', () => {
56+
expect(calls).toEqual([1, 2, 3, 4])
57+
done()
58+
})
59+
})
60+
61+
it('replace abort', done => {
62+
router.replace('/foo', spy1, spy2)
63+
router.replace('/bar', () => {
64+
expect(calls).toEqual([1, 1, 2, 2])
65+
expect(spy1).not.toHaveBeenCalled()
66+
expect(spy2).toHaveBeenCalled()
67+
done()
68+
})
69+
})
70+
})

Diff for: types/router.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ declare class VueRouter {
2222

2323
beforeEach (guard: NavigationGuard): void;
2424
afterEach (hook: (to: Route, from: Route) => any): void;
25-
push (location: RawLocation): void;
26-
replace (location: RawLocation): void;
25+
push (location: RawLocation, onComplete?: Function, onAbort?: Function): void;
26+
replace (location: RawLocation, onComplete?: Function, onAbort?: Function): void;
2727
go (n: number): void;
2828
back (): void;
2929
forward (): void;

0 commit comments

Comments
 (0)