Skip to content

Commit 4650218

Browse files
committed
Cleaned up router code, fixed instances not getting destroyed.
1 parent fecffa9 commit 4650218

File tree

7 files changed

+81
-82
lines changed

7 files changed

+81
-82
lines changed

platform/nativescript/framework.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ global.process.env = global.process.env || {}
1010
import Vue from './runtime/index'
1111
import ModalPlugin from './plugins/modal-plugin'
1212
import NavigatorPlugin from './plugins/navigator-plugin'
13-
import DecoderPlugin from './plugins/decoder-plugin'
13+
// import DecoderPlugin from './plugins/decoder-plugin'
1414
import RouterPlugin from './plugins/router-plugin'
1515

1616
Vue.use(ModalPlugin)
1717
Vue.use(NavigatorPlugin)
18-
Vue.use(DecoderPlugin)
18+
// Vue.use(DecoderPlugin)
1919
Vue.use(RouterPlugin)
2020

2121
export default Vue

platform/nativescript/plugins/navigator-plugin.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@ import { Page } from 'ui/page'
33
import { topmost } from 'ui/frame'
44
import { start } from 'application'
55
import { VUE_VM_REF } from '../runtime/index'
6+
import { after } from '../util'
67

78
export default {
89
install(Vue) {
910
Vue.navigateBack = Vue.prototype.$navigateBack = function() {
10-
const frame = topmost()
11-
frame.eachChildView(child => {
12-
const vm = child[VUE_VM_REF]
13-
14-
if (vm && !vm.__is_root__) {
15-
vm.$destroy()
16-
}
17-
})
18-
19-
return frame.goBack()
11+
return topmost().goBack()
2012
}
13+
2114
Vue.navigateTo = Vue.prototype.$navigateTo = function(component, options) {
2215
return new Promise(resolve => {
2316
const placeholder = Vue.$document.createComment('placeholder')
@@ -42,6 +35,16 @@ export default {
4235
Object.assign(
4336
{
4437
create: () => {
38+
if (frame) {
39+
toPage.disposeNativeView = after(
40+
toPage.disposeNativeView,
41+
toPage,
42+
() => {
43+
vm.$destroy()
44+
}
45+
)
46+
}
47+
4548
resolve(toPage)
4649
return toPage
4750
}

platform/nativescript/plugins/router-plugin.js

Lines changed: 38 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,48 @@
1-
import { android, AndroidApplication } from 'application'
1+
import { Page } from 'ui/page'
2+
import { before, trace } from '../util/index'
23

3-
export default {
4-
install(Vue) {
5-
const patchGo = router => {
6-
if (router.go.__patched__) {
7-
return
8-
}
9-
10-
if (router.history.index === -1) {
11-
// Todo: make sure this is indeed required
12-
// The problem: When using router.replace() to set the initial route
13-
// the history index stays -1, which then causes an issue when visiting a route,
14-
// going back, and then trying to visit again (the active route is not changed on nav back)
15-
// This fixes it, since it allows the router.go logic to run
16-
router.history.index = 0
17-
}
18-
19-
const go = router.go
20-
21-
router.go = n => {
22-
if (router.isPageRouter && n < 0) {
23-
for (let i = n; i < 0; ++i) {
24-
// Todo: figure out a cleaner way to navigate back n frames
25-
Vue.navigateBack()
26-
}
27-
}
28-
29-
go.call(router, n)
30-
}
4+
export function patchRouter(router, Vue) {
5+
if (router.__patched_for_page_routing__) {
6+
return
7+
}
8+
router.__patched_for_page_routing__ = true
9+
10+
// The problem: When using router.replace() to set the initial route
11+
// the history index stays -1, which then causes an issue when visiting a route,
12+
// going back, and then trying to visit again (the active route is not changed on nav back)
13+
// This fixes it, since it allows the router.go logic to run
14+
router.history.index = 0
15+
16+
router.go = before(router.go, router, n => {
17+
if (n === -1) {
18+
router.isBackNavigation = true
19+
Vue.navigateBack()
20+
}
21+
})
3122

32-
router.go.__patched__ = true
23+
router.afterEach(() => {
24+
if (router.isBackNavigation) {
25+
router.isBackNavigation = false
26+
return
3327
}
3428

35-
const registerGuards = router => {
36-
if (router.__patched__) {
37-
return
38-
}
29+
const component = router.getMatchedComponents()[0]
3930

40-
router.afterEach(() => {
41-
if (!router.isPageRouter) {
42-
return
31+
Vue.navigateTo(component, {
32+
context: { router }
33+
}).then(page => {
34+
page.off(Page.navigatingFromEvent)
35+
page.on(Page.navigatingFromEvent, args => {
36+
if (args.isBackNavigation) {
37+
router.back()
4338
}
44-
45-
const component = router.getMatchedComponents()[0]
46-
47-
Vue.navigateTo(component, {
48-
context: { router }
49-
})
5039
})
40+
})
41+
})
42+
}
5143

52-
router.__patched__ = true
53-
}
54-
44+
export default {
45+
install(Vue) {
5546
Vue.mixin({
5647
beforeCreate() {
5748
if (!this.$options.router) {
@@ -62,28 +53,7 @@ export default {
6253
const router = this.$options.router
6354
const self = this
6455

65-
// Handle back button on android
66-
if (android && !android.__patched__) {
67-
android.on(AndroidApplication.activityBackPressedEvent, e => {
68-
if (router.history.index !== 0) {
69-
e.cancel = true
70-
}
71-
router.back()
72-
})
73-
74-
android.__patched__ = true
75-
}
76-
77-
if (!router.hasOwnProperty('isPageRouter')) {
78-
Object.defineProperty(router, 'isPageRouter', {
79-
get() {
80-
return self.$options.pageRouting
81-
}
82-
})
83-
}
84-
85-
patchGo(router)
86-
registerGuards(router)
56+
patchRouter(router, Vue)
8757

8858
// Overwrite the default $start function
8959
this.$start = () => {

platform/nativescript/util/entity-decoder.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export default {
2+
decode
3+
}
4+
15
export function decode(html) {
26
// todo?
37
return html

platform/nativescript/util/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,21 @@ export function trace(message) {
3939
`{NSVue (Vue: ${VUE_VERSION} | NSVue: ${NS_VUE_VERSION})} -> ${message}`
4040
)
4141
}
42+
43+
export function before(original, thisArg, wrap) {
44+
const __slice = Array.prototype.slice
45+
return function() {
46+
const args = __slice.call(arguments)
47+
wrap.apply(null, args)
48+
original.apply(thisArg, args)
49+
}
50+
}
51+
52+
export function after(original, thisArg, wrap) {
53+
const __slice = Array.prototype.slice
54+
return function() {
55+
const args = __slice.call(arguments)
56+
wrap.apply(null, args)
57+
original.apply(thisArg, args)
58+
}
59+
}

rollup.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const aliases = {
2828
core: resolveVue('core'),
2929
shared: resolveVue('shared'),
3030
sfc: resolveVue('sfc'),
31-
he: path.resolve(__dirname, 'node_modules', 'he', 'he')
31+
//he: path.resolve(__dirname, 'node_modules', 'he', 'he')
32+
he: path.resolve(__dirname, 'platform/nativescript/util/entity-decoder')
3233
}
3334

3435
export default {
@@ -77,7 +78,7 @@ export default {
7778
commonjs({
7879
include: [
7980
path.resolve(__dirname, 'node_modules', 'vue') + '/**',
80-
path.resolve(__dirname, 'node_modules', 'he') + '/**',
81+
//path.resolve(__dirname, 'node_modules', 'he') + '/**',
8182
],
8283

8384
namedExports: {

samples/app/app-with-router-pages.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Foo = {
77
<page>
88
<action-bar :title="$route.path"></action-bar>
99
<stack-layout>
10+
<label :text="$route.path" textWrap="true"></label>
1011
<label style="text-align: center; color: #41b883; font-size: 30">Hi I'm foo!</label>
1112
<button @tap="$router.push('/bar')">Bar</button>
1213
<button @tap="$router.push('/baz')">Baz</button>
@@ -22,6 +23,7 @@ const Bar = {
2223
</action-bar>
2324
<stack-layout>
2425
<label style="text-align: center; color: #41b883; font-size: 30">Hi I'm bar!</label>
26+
<button @tap="$router.push('/baz')">Baz</button>
2527
</stack-layout>
2628
</page>
2729
`
@@ -34,6 +36,7 @@ const Baz = {
3436
</action-bar>
3537
<stack-layout>
3638
<label style="text-align: center; color: #41b883; font-size: 30">Hi I'm baz!</label>
39+
<button @tap="$router.push('/bar')">Bar</button>
3740
</stack-layout>
3841
</page>
3942
`

0 commit comments

Comments
 (0)
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