Skip to content

Commit

Permalink
[Feature Router Service] routeWillChange & routeDidChange
Browse files Browse the repository at this point in the history
  • Loading branch information
chadhietala committed Oct 1, 2018
1 parent 7165960 commit 38c3c3b
Show file tree
Hide file tree
Showing 8 changed files with 750 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"puppeteer": "^1.3.0",
"qunit": "^2.5.0",
"route-recognizer": "^0.3.4",
"router_js": "^5.0.3",
"router_js": "^5.1.1",
"rsvp": "^4.8.2",
"semver": "^5.5.0",
"serve-static": "^1.12.2",
Expand Down
14 changes: 13 additions & 1 deletion packages/@ember/-internals/routing/lib/services/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Evented } from '@ember/-internals/runtime';
import { readOnly } from '@ember/object/computed';
import Service from '@ember/service';
import { Transition } from 'router_js';
import EmberRouter from '../system/router';
import { extractRouteArgs, resemblesURL, shallowEqual } from '../utils';

Expand Down Expand Up @@ -148,7 +150,17 @@ export default class RouterService extends Service {
}
}

RouterService.reopen({
RouterService.reopen(Evented, {
init() {
this._super(...arguments);
this._router.on('routeWillChange', (transition: Transition) => {
this.trigger('routeWillChange', transition);
});

this._router.on('routeDidChange', (transition: Transition) => {
this.trigger('routeDidChange', transition);
});
},
/**
Name of the current route.
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/-internals/routing/lib/system/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ class Route extends EmberObject implements IRoute {
/**
@private
@method _reset
@method _internalReset
@since 1.7.0
*/
_internalReset(isExiting: boolean, transition: Transition) {
Expand Down
67 changes: 65 additions & 2 deletions packages/@ember/-internals/routing/lib/system/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getOwner, Owner } from '@ember/-internals/owner';
import { A as emberA, Evented, Object as EmberObject, typeOf } from '@ember/-internals/runtime';
import { EMBER_ROUTING_ROUTER_SERVICE } from '@ember/canary-features';
import { assert, deprecate, info } from '@ember/debug';
import { HANDLER_INFOS } from '@ember/deprecated-features';
import { HANDLER_INFOS, ROUTER_EVENTS } from '@ember/deprecated-features';
import EmberError from '@ember/error';
import { assign } from '@ember/polyfills';
import { cancel, once, run, scheduleOnce } from '@ember/runloop';
Expand All @@ -21,7 +21,9 @@ import { MatchCallback } from 'route-recognizer';
import Router, {
InternalRouteInfo,
InternalTransition,
logAbort,
Transition,
TransitionError,
TransitionState,
} from 'router_js';
import { EngineRouteInfo } from './engines';
Expand Down Expand Up @@ -257,6 +259,34 @@ class EmberRouter extends EmberObject {
return triggerEvent.bind(router)(routeInfos, ignoreFailure, name, args);
}

routeWillChange(transition: Transition) {
if (EMBER_ROUTING_ROUTER_SERVICE) {
router.trigger('routeWillChange', transition);
}
}

routeDidChange(transition: Transition) {
if (EMBER_ROUTING_ROUTER_SERVICE) {
router.trigger('routeDidChange', transition);
}
}

transitionDidError(error: TransitionError, transition: Transition) {
if (error.wasAborted || transition.isAborted) {
return logAbort(transition);
} else {
transition.trigger(false, 'error', error.error, transition, error.route);
if (router._isErrorHandled(error.error)) {
transition.rollback();
this.routeDidChange(transition);
return error.error;
} else {
transition.abort();
return error.error;
}
}
}

_triggerWillChangeContext() {
return router;
}
Expand Down Expand Up @@ -1449,7 +1479,7 @@ export function triggerEvent(
routeInfos: PrivateRouteInfo[],
ignoreFailure: boolean,
name: string,
args: unknown[]
args: any[]
) {
if (!routeInfos) {
if (ignoreFailure) {
Expand Down Expand Up @@ -1793,4 +1823,37 @@ EmberRouter.reopen(Evented, {
}),
});

if (EMBER_ROUTING_ROUTER_SERVICE) {
if (ROUTER_EVENTS) {
EmberRouter.reopen({
on(name: string) {
this._super(...arguments);
let hasDidTransition = name === 'didTransition';
let hasWillTransition = name === 'willTransition';

if (hasDidTransition) {
deprecate(
'You attempted to listen to the "didTransition" event which is deprecated. Please inject the router service and listen to the "routeDidChange" event.',
false,
{
id: 'deprecate-router-events',
until: '3.9.0',
}
);
}

if (hasWillTransition) {
deprecate(
'You attempted to listen to the "willTransition" event which is deprecated. Please inject the router service and listen to the "routeWillChange" event.',
false,
{
id: 'deprecate-router-events',
until: '3.9.0',
}
);
}
},
});
}
}
export default EmberRouter;
1 change: 1 addition & 0 deletions packages/@ember/deprecated-features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export const MAP = !!'3.3.0-beta.1';
export const ORDERED_SET = !!'3.3.0-beta.1';
export const MERGE = !!'3.6.0-beta.1';
export const HANDLER_INFOS = !!'3.9.0';
export const ROUTER_EVENTS = !!'3.9.0';
Loading

0 comments on commit 38c3c3b

Please sign in to comment.