/
redirect-patch.js
68 lines (56 loc) · 2.19 KB
/
redirect-patch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import EmberRouter from '@ember/routing/router';
import { lte } from 'ember-compatibility-helpers';
let hasInitialized = false;
export function initialize() {
if (lte('3.5.1')) {
// Delete all of this in the Ember 3.8 LTS and rev major
if (!hasInitialized) {
hasInitialized = true;
EmberRouter.reopen({
_initRouterJs() {
this._super.apply(this, arguments);
// now this.router is available
// router.router renamed to _routerMicrolib in 2.13
// https://emberjs.com/deprecations/v2.x/#toc_ember-router-router-renamed-to-ember-router-_routermicrolib
const router = this._routerMicrolib || this.router;
const emberRouter = this;
const stack = [];
let latest = null;
// replace router's transitionByIntent method, through which all transitions pass
const oldTransitionByIntent = router.transitionByIntent;
router.transitionByIntent = function() {
stack.push(1);
const oldInfos = router.state.handlerInfos;
const transition = oldTransitionByIntent.apply(router, arguments);
if (!latest || stack.length >= latest.stackSize) {
latest = {
oldInfos,
transition,
};
}
latest.stackSize = stack.length;
// Router.js does not trigger `willTransition` when redirecting. We need
// `willTransition` to be triggered regardless so that the `prefetch` hook is
// always invoked for the routes in the new transition. Internally, the
// `willTransition` hook uses `Ember.run.once` to fire the event, which
// gurantees that it will not trigger `willTransition` multiple times.
if (stack.length === 1 && transition) {
emberRouter.willTransition(
latest.oldInfos,
transition.state.handlerInfos,
latest.transition
);
latest = null;
}
stack.pop();
return transition;
};
},
});
}
}
}
export default {
name: 'redirect-patch',
initialize: initialize,
};