Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC release] Add note about router service event and property update… #18738

Merged
merged 1 commit into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/@ember/-internals/routing/lib/services/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,16 @@ export default class RouterService extends Service {
}
```

`routeDidChange` will be called after any `Route`'s
[didTransition](/ember/release/classes/Route/events/didTransition?anchor=didTransition)
action has been fired.
The updates of properties
[currentURL](/ember/release/classes/RouterService/properties/currentURL?anchor=currentURL),
[currentRouteName](/ember/release/classes/RouterService/properties/currentURL?anchor=currentRouteName)
and
[currentRoute](/ember/release/classes/RouterService/properties/currentURL?anchor=currentRoute)
are completed at the time `routeDidChange` is called.

@event routeDidChange
@param {Transition} transition
@public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,98 @@ let ROUTE_NAMES = ['index', 'child', 'sister', 'brother', 'loading'];
let InstrumentedRoute = Route.extend({
routerService: injectService('router'),

init() {
this._super(...arguments);
let service = get(this, 'routerService');
service.on('routeWillChange', transition => {
results.push([
service.get('currentRouteName'),
`${this.routeName} routeWillChange: ${transition.from && transition.from.name} - ${
transition.to.name
}`,
service.get('currentURL'),
]);
});
service.on('routeDidChange', transition => {
results.push([
service.get('currentRouteName'),
`${this.routeName} routeDidChange: ${transition.from && transition.from.name} - ${
transition.to.name
}`,
service.get('currentURL'),
]);
});
},

activate() {
let service = get(this, 'routerService');
results.push([
service.get('currentRouteName'),
`${this.routeName} activate`,
service.get('currentURL'),
]);
},

redirect() {
let service = get(this, 'routerService');
results.push([
service.get('currentRouteName'),
`${this.routeName} redirect`,
service.get('currentURL'),
]);
},

beforeModel() {
let service = get(this, 'routerService');
results.push([service.get('currentRouteName'), 'beforeModel', service.get('currentURL')]);
results.push([
service.get('currentRouteName'),
`${this.routeName} beforeModel`,
service.get('currentURL'),
]);
},

model() {
let service = get(this, 'routerService');
results.push([service.get('currentRouteName'), 'model', service.get('currentURL')]);
results.push([
service.get('currentRouteName'),
`${this.routeName} model`,
service.get('currentURL'),
]);
return new RSVP.Promise(resolve => {
setTimeout(resolve, 200);
});
},

afterModel() {
let service = get(this, 'routerService');
results.push([service.get('currentRouteName'), 'afterModel', service.get('currentURL')]);
results.push([
service.get('currentRouteName'),
`${this.routeName} afterModel`,
service.get('currentURL'),
]);
},

actions: {
willTransition(transition) {
let service = get(this, 'routerService');
results.push([
service.get('currentRouteName'),
`${this.routeName} willTransition: ${transition.from && transition.from.name} - ${
transition.to.name
}`,
service.get('currentURL'),
]);
return true;
},
didTransition() {
let service = get(this, 'routerService');
results.push([
service.get('currentRouteName'),
`${this.routeName} didTransition`,
service.get('currentURL'),
]);
return true;
},
},
});

Expand Down Expand Up @@ -105,15 +181,26 @@ moduleFor(
});
}

['@test RouterService#currentURL is not set during lifecycle hooks'](assert) {
['@test RouterService#currentURL is not set during model lifecycle hooks until routeDidChange'](
assert
) {
assert.expect(2);

return this.visit('/')
.then(() => {
assert.deepEqual(results, [
[null, 'beforeModel', null],
[null, 'model', null],
['parent.loading', 'afterModel', '/'],
[null, 'parent.index routeWillChange: null - parent.index', null],
[null, 'parent.index beforeModel', null],
[null, 'parent.index model', null],
[null, 'parent.loading activate', null],
[null, 'parent.loading routeWillChange: null - parent.loading', null],
[null, 'parent.index routeWillChange: null - parent.loading', null],
['parent.loading', 'parent.index afterModel', '/'],
['parent.loading', 'parent.index redirect', '/'],
['parent.loading', 'parent.index activate', '/'],
['parent.loading', 'parent.index didTransition', '/'],
['parent.index', 'parent.loading routeDidChange: null - parent.index', '/'],
['parent.index', 'parent.index routeDidChange: null - parent.index', '/'],
]);

results = [];
Expand All @@ -122,9 +209,27 @@ moduleFor(
})
.then(() => {
assert.deepEqual(results, [
['parent.index', 'beforeModel', '/'],
['parent.index', 'model', '/'],
['parent.loading', 'afterModel', '/child'],
['parent.index', 'parent.index willTransition: parent.index - parent.child', '/'],
['parent.index', 'parent.child routeWillChange: parent.index - parent.child', '/'],
['parent.index', 'parent.loading routeWillChange: parent.index - parent.child', '/'],
['parent.index', 'parent.index routeWillChange: parent.index - parent.child', '/'],
['parent.index', 'parent.child beforeModel', '/'],
['parent.index', 'parent.child model', '/'],
['parent.index', 'parent.loading activate', '/'],
['parent.index', 'parent.child routeWillChange: parent.index - parent.loading', '/'],
['parent.index', 'parent.loading routeWillChange: parent.index - parent.loading', '/'],
['parent.index', 'parent.index routeWillChange: parent.index - parent.loading', '/'],
['parent.loading', 'parent.child afterModel', '/child'],
['parent.loading', 'parent.child redirect', '/child'],
['parent.loading', 'parent.child activate', '/child'],
['parent.loading', 'parent.child didTransition', '/child'],
['parent.child', 'parent.child routeDidChange: parent.index - parent.child', '/child'],
[
'parent.child',
'parent.loading routeDidChange: parent.index - parent.child',
'/child',
],
['parent.child', 'parent.index routeDidChange: parent.index - parent.child', '/child'],
]);
});
}
Expand Down