Skip to content

Commit 5ed4204

Browse files
committed
feat: allow passing frame to $navigateTo
BREAKING CHANGE: the options object of $navigateTo has been simplified fix nativescript-vue#213
1 parent f723d42 commit 5ed4204

File tree

4 files changed

+82
-37
lines changed

4 files changed

+82
-37
lines changed

platform/nativescript/plugins/navigator-plugin.js

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,52 @@
1-
import { topmost } from 'tns-core-modules/ui/frame'
2-
import { ensurePage } from '../util'
1+
import { isObject, isDef, isPrimitive } from 'shared/util'
2+
import { getFrame } from '../util/frame'
3+
import { getFrameById } from 'tns-core-modules/ui/frame'
4+
5+
function getFrameInstance(frame) {
6+
// get the frame that we need to navigate
7+
// this can be a frame id (String)
8+
// a Vue ref to a frame
9+
// a Frame ViewNode
10+
// or a Frame instance
11+
if (isObject(frame) && isDef(frame.$el)) {
12+
frame = frame.$el.nativeView
13+
} else if (isPrimitive(frame)) {
14+
frame = getFrameById(frame)
15+
} else if (isDef(frame.nativeView)) {
16+
frame = frame.nativeView
17+
}
18+
// finally get the component instance for this frame
19+
return getFrame(frame.id)
20+
}
321

422
export default {
523
install(Vue) {
6-
Vue.navigateBack = Vue.prototype.$navigateBack = function() {
7-
return topmost().goBack()
8-
}
24+
Vue.prototype.$navigateBack = function(options) {
25+
const defaultOptions = {
26+
frame: 'default'
27+
}
28+
options = Object.assign({}, defaultOptions, options)
29+
const frame = getFrameInstance(options.frame)
930

10-
Vue.navigateTo = Vue.prototype.$navigateTo = function(
11-
component,
12-
options = {},
13-
pageCb = () => {}
14-
) {
15-
return new Promise(resolve => {
16-
const navigationEntry = {
17-
create() {
18-
const vm = new (Vue.extend(component))(options.context)
19-
20-
vm.$mount()
21-
const pageInstance = ensurePage(vm.$el, vm)
31+
frame.back()
32+
}
2233

23-
pageCb(pageInstance)
24-
resolve(pageInstance)
34+
Vue.prototype.$navigateTo = function(component, options) {
35+
const defaultOptions = {
36+
frame: 'default'
37+
}
38+
// build options object with defaults
39+
options = Object.assign({}, defaultOptions, options)
2540

26-
return pageInstance
27-
}
28-
}
41+
return new Promise(resolve => {
42+
const frame = getFrameInstance(options.frame)
43+
const page = new Vue({
44+
parent: this,
45+
render: h => h(component, { props: options.props })
46+
}).$mount().$el.nativeView
2947

30-
topmost().navigate(Object.assign({}, navigationEntry, options))
48+
frame.navigate(Object.assign({}, options, { create: () => page }))
49+
resolve(page)
3150
})
3251
}
3352
}

platform/nativescript/plugins/router-plugin.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,22 @@ export function patchRouter(router, Vue) {
8080

8181
const component = router.getMatchedComponents()[0]
8282

83-
Vue.navigateTo(component, {
84-
context: { router },
85-
transition: router.pageTransition
86-
// Todo: add transitionAndroid and transitionIOS
87-
}).then(page => {
88-
router.pageStack.push(page)
89-
90-
page.on(Page.navigatedFromEvent, ({ isBackNavigation }) => {
91-
if (isBackNavigation && !router.isBackNavigation) {
92-
router._beginBackNavigation(false)
93-
router.back()
94-
}
83+
router.app
84+
.$navigateTo(component, {
85+
context: { router },
86+
transition: router.pageTransition
87+
// Todo: add transitionAndroid and transitionIOS
88+
})
89+
.then(page => {
90+
router.pageStack.push(page)
91+
92+
page.on(Page.navigatedFromEvent, ({ isBackNavigation }) => {
93+
if (isBackNavigation && !router.isBackNavigation) {
94+
router._beginBackNavigation(false)
95+
router.back()
96+
}
97+
})
9598
})
96-
})
9799
})
98100
}
99101

platform/nativescript/runtime/components/frame.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
import { setFrame, deleteFrame } from '../../util/frame'
2+
13
export default {
24
name: 'frame',
5+
props: {
6+
id: {
7+
default: 'default'
8+
}
9+
},
310
created() {
11+
setFrame(this.$props.id, this)
412
this.cache = {}
513
},
14+
destroyed() {
15+
deleteFrame(this.$props.id)
16+
},
617
render(h) {
718
return h(
819
'NativeFrame',
920
{
10-
attrs: this.$attrs,
21+
attrs: Object.assign({}, this.$attrs, this.$props),
1122
on: this.$listeners
1223
},
1324
this.$slots.default

platform/nativescript/util/frame.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const frames = new Map()
2+
3+
export function setFrame(id, frame) {
4+
return frames.set(id, frame)
5+
}
6+
7+
export function getFrame(id) {
8+
return frames.get(id)
9+
}
10+
11+
export function deleteFrame(id) {
12+
return frames.delete(id)
13+
}

0 commit comments

Comments
 (0)