UNPKG

4.79 kBJavaScriptView Raw
1import Vue from "nativescript-vue";
2import { Page } from "@nativescript/core/ui/page";
3export default {
4 nextCallbacks: [],
5 mounted() {
6 if (!this.nativeView.eventsSet && this.isPage()) {
7 this.nativeView.eventsSet = true;
8 this.nativeView.on(Page.navigatingFromEvent, this.onNavigatingFrom.bind(this));
9 this.nativeView.on(Page.navigatingToEvent, this.onNavigatingTo.bind(this));
10 this.nativeView.on(Page.navigatedToEvent, this.onNavigatedTo.bind(this));
11 this.nativeView.on(Page.navigatedFromEvent, this.onNavigatedFrom.bind(this));
12 }
13 },
14 destroyed() {
15 if (this.nativeView.eventsSet && this.isPage()) {
16 this.nativeView.off(Page.navigatedToEvent, this.onNavigatedTo.bind(this));
17 this.nativeView.off(Page.navigatingToEvent, this.onNavigatingTo.bind(this));
18 this.nativeView.off(Page.navigatingFromEvent, this.onNavigatingFrom.bind(this));
19 this.nativeView.off(Page.navigatedFromEvent, this.onNavigatedFrom.bind(this));
20 }
21 },
22 methods: {
23 onNavigatingFrom(data) {
24 if (data.object.frame &&
25 data.object.frame.parent &&
26 data.object.frame.parent.modal) {
27 Vue.prototype.$modalPage = {
28 data,
29 instance: this,
30 };
31 return;
32 }
33 const to = this.$router.getNewRoute();
34 const from = this.$router.getCurrentRoute();
35 if (this.$options.beforeRouteLeave) {
36 this.$options.beforeRouteLeave.call(this, to, from, data.object.navigationContext);
37 }
38 if (this.$options.navigatingFrom) {
39 console.warn("ROUTER", "navigatingFrom() is deprecated. Use beforeRouteLeave() instead");
40 this.$options.navigatingFrom.call(this, to, from, data.object.navigationContext);
41 }
42 },
43 onNavigatingTo(data) {
44 const to = this.$router.getNewRoute();
45 const from = this.$router.getCurrentRoute();
46 if (this.$options.beforeRouteUpdate && to && from && to.path === from.path) {
47 this.$options.beforeRouteUpdate.call(this, to, from, data.object.navigationContext);
48 }
49 if (to.beforeEnter && typeof to.beforeEnter === "function") {
50 const next = (vmContext) => {
51 if (typeof vmContext === "undefined") {
52 return;
53 }
54 if (typeof vmContext === "function") {
55 this.$options.nextCallbacks.push(vmContext);
56 }
57 };
58 this.$options.beforeEnter.call(this, to, from, next, data.object.navigationContext);
59 }
60 if (this.$options.beforeRouteEnter) {
61 const next = (vmContext) => {
62 if (typeof vmContext === "undefined") {
63 return;
64 }
65 if (typeof vmContext === "function") {
66 this.$options.nextCallbacks.push(vmContext);
67 }
68 };
69 this.$options.beforeRouteEnter.call(this, to, from, next, data.object.navigationContext);
70 }
71 this.$router.invokeBeforeResolve();
72 if (this.$options.navigatingTo) {
73 console.warn("ROUTER", "navigatingTo() is deprecated. Use beforeRouteEnter() instead");
74 this.$options.navigatingTo.call(this, to, from, data.object.navigationContext);
75 }
76 },
77 onNavigatedFrom(data) {
78 if (this.$options.navigatedFrom) {
79 console.warn("ROUTER", "navigatedFrom() is deprecated. For store state updates use meta dispatcher instead. For component state you could opt for beforeRouteLeave()");
80 this.$options.navigatedFrom.call(this, data.object.navigationContext);
81 }
82 },
83 onNavigatedTo(data) {
84 this.$router.invokeAfterEach();
85 this.$options.nextCallbacks.forEach((callback) => {
86 callback.call(this, this, data.object.navigationContext);
87 });
88 this.$options.nextCallbacks.splice(0);
89 if (this.$options.navigatedTo) {
90 console.warn("ROUTER", 'navigatedTo() is deprecated. Use beforeRouteEnter() with "next(vm => ...)" callback instead');
91 this.$options.navigatedTo.call(this, data.object.navigationContext);
92 }
93 },
94 isPage() {
95 if (this.nativeView.__vuePageRef__) {
96 return true;
97 }
98 return false;
99 },
100 },
101};
102//# sourceMappingURL=router-mixin.js.map
\No newline at end of file