8000 This should fix the back navigation navigating all the way back to th… · rdlauer/nativescript-vue@0bbc07b · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bbc07b

Browse files
committed
This should fix the back navigation navigating all the way back to the initial page. Should fix nativescript-vue#55
1 parent ade738b commit 0bbc07b

File tree

1 file changed

+53
-8
lines changed

1 file changed

+53
-8
lines changed

platform/nativescript/plugins/router-plugin.js

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Page } from 'ui/page'
2-
import { before, trace } from '../util/index'
2+
import { topmost } from 'ui/frame'
3+
import { before } from '../util/index'
34

45
export function patchRouter(router, Vue) {
56
if (router.__patched_for_page_routing__) {
@@ -13,16 +14,55 @@ export function patchRouter(router, Vue) {
1314
// This fixes it, since it allows the router.go logic to run
1415
router.history.index = 0
1516

17+
// initial navigation states
18+
router.isBackNavigation = false
19+
router.shouldNavigate = true
20+
router.pageStack = []
21+
22+
router._beginBackNavigation = (shouldNavigate = true) => {
23+
if (router.isBackNavigation) {
24+
throw new Error(
25+
'router._beginBackNavigation was called while already navigating back.'
26+
)
27+
}
28+
29+
router.isBackNavigation = true
30+
router.shouldNavigate = shouldNavigate
31+
}
32+
33+
router._finishBackNavigation = () => {
34+
if (!router.isBackNavigation) {
35+
throw new Error(
36+
'router._finishBackNavigation was called while there was no back navigation.'
37+
)
38+
}
39+
40+
router.isBackNavigation = false
41+
}
42+
1643
router.go = before(router.go, router, n => {
17-
if (n === -1) {
18-
router.isBackNavigation = true
19-
Vue.navigateBack()
44+
if (n === -1 && !router.isBackNavigation) {
45+
router._beginBackNavigation()
2046
}
2147
})
2248

2349
router.afterEach(() => {
2450
if (router.isBackNavigation) {
25-
router.isBackNavigation = false
51+
if (router.shouldNavigate) {
52+
Vue.navigateBack()
53+
}
54+
router.pageStack.pop()
55+
const page = router.pageStack[router.pageStack.length - 1]
56+
57+
const callback = ({ isBackNavigation }) => {
58+
if (isBackNavigation) {
59+
router._finishBackNavigation()
60+
}
61+
page.off(Page.navigatedToEvent, callback)
62+
}
63+
64+
page.on(Page.navigatedToEvent, callback)
65+
2666
return
2767
}
2868

@@ -31,9 +71,11 @@ export function patchRouter(router, Vue) {
3171
Vue.navigateTo(component, {
3272
context: { router }
3373
}).then(page => {
34-
page.off(Page.navigatingFromEvent)
35-
page.on(Page.navigatingFromEvent, args => {
36-
if (args.isBackNavigation) {
74+
router.pageStack.push(page)
75+
76+
page.on(Page.navigatedFromEvent, ({ isBackNavigation }) => {
77+
if (isBackNavigation && !router.isBackNavigation) {
78+
router._beginBackNavigation(false)
3779
router.back()
3880
}
3981
})
@@ -69,6 +111,9 @@ export default {
69111
this.$navigateTo(initial, {
70112
context: { router },
71113
clearHistory: true
114+
}).then(page => {
115+
// Todo: this callback never fires on iOS and causes an issue
116+
router.pageStack.push(page)
72117
})
73118
}
74119
}

0 commit comments

Comments
 (0)
0