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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): update route snapshot before emit new values #13558

Merged
merged 1 commit into from
Dec 27, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions modules/@angular/router/src/router_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,22 +330,23 @@ function serializeNode(node: TreeNode<ActivatedRouteSnapshot>): string {
*/
export function advanceActivatedRoute(route: ActivatedRoute): void {
if (route.snapshot) {
if (!shallowEqual(route.snapshot.queryParams, route._futureSnapshot.queryParams)) {
const currentSnapshot = route.snapshot;
route.snapshot = route._futureSnapshot;
if (!shallowEqual(currentSnapshot.queryParams, route._futureSnapshot.queryParams)) {
(<any>route.queryParams).next(route._futureSnapshot.queryParams);
}
if (route.snapshot.fragment !== route._futureSnapshot.fragment) {
if (currentSnapshot.fragment !== route._futureSnapshot.fragment) {
(<any>route.fragment).next(route._futureSnapshot.fragment);
}
if (!shallowEqual(route.snapshot.params, route._futureSnapshot.params)) {
if (!shallowEqual(currentSnapshot.params, route._futureSnapshot.params)) {
(<any>route.params).next(route._futureSnapshot.params);
}
if (!shallowEqualArrays(route.snapshot.url, route._futureSnapshot.url)) {
if (!shallowEqualArrays(currentSnapshot.url, route._futureSnapshot.url)) {
(<any>route.url).next(route._futureSnapshot.url);
}
if (!equalParamsAndUrlSegments(route.snapshot, route._futureSnapshot)) {
if (!equalParamsAndUrlSegments(currentSnapshot, route._futureSnapshot)) {
(<any>route.data).next(route._futureSnapshot.data);
}
route.snapshot = route._futureSnapshot;
} else {
route.snapshot = route._futureSnapshot;

Expand All @@ -358,4 +359,4 @@ export function advanceActivatedRoute(route: ActivatedRoute): void {
export function equalParamsAndUrlSegments(
a: ActivatedRouteSnapshot, b: ActivatedRouteSnapshot): boolean {
return shallowEqual(a.params, b.params) && equalSegments(a.url, b.url);
}
}
22 changes: 17 additions & 5 deletions modules/@angular/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,17 @@ describe('Integration', () => {
const user = fixture.debugElement.children[1].children[1].componentInstance;

expect(team.recordedParams).toEqual([{id: '22'}]);
expect(team.snapshotParams).toEqual([{id: '22'}]);
expect(user.recordedParams).toEqual([{name: 'victor'}]);
expect(user.snapshotParams).toEqual([{name: 'victor'}]);

router.navigateByUrl('/team/22/user/fedor');
advance(fixture);

expect(team.recordedParams).toEqual([{id: '22'}]);
expect(team.snapshotParams).toEqual([{id: '22'}]);
expect(user.recordedParams).toEqual([{name: 'victor'}, {name: 'fedor'}]);
expect(user.snapshotParams).toEqual([{name: 'victor'}, {name: 'fedor'}]);
})));

it('should work when navigating to /', fakeAsync(inject([Router], (router: Router) => {
Expand Down Expand Up @@ -2631,11 +2635,15 @@ class BlankCmp {
class TeamCmp {
id: Observable<string>;
recordedParams: Params[] = [];
snapshotParams: Params[] = [];
routerLink = ['.'];

constructor(public route: ActivatedRoute) {
this.id = map.call(route.params, (p: any) => p['id']);
route.params.forEach(_ => this.recordedParams.push(_));
route.params.forEach(p => {
this.recordedParams.push(p);
this.snapshotParams.push(route.snapshot.params);
});
}
}

Expand All @@ -2651,10 +2659,14 @@ class TwoOutletsCmp {
class UserCmp {
name: Observable<string>;
recordedParams: Params[] = [];
snapshotParams: Params[] = [];

constructor(route: ActivatedRoute) {
this.name = map.call(route.params, (p: any) => p['name']);
route.params.forEach(_ => this.recordedParams.push(_));
route.params.forEach(p => {
this.recordedParams.push(p);
this.snapshotParams.push(route.snapshot.params);
});
}
}

Expand Down Expand Up @@ -2777,7 +2789,7 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
RootCmp,
RelativeLinkInIfCmp,
RootCmpWithTwoOutlets,
EmptyQueryParamsCmp
EmptyQueryParamsCmp,
],


Expand All @@ -2803,7 +2815,7 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
RootCmp,
RelativeLinkInIfCmp,
RootCmpWithTwoOutlets,
EmptyQueryParamsCmp
EmptyQueryParamsCmp,
],


Expand All @@ -2830,7 +2842,7 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
RootCmp,
RelativeLinkInIfCmp,
RootCmpWithTwoOutlets,
EmptyQueryParamsCmp
EmptyQueryParamsCmp,
]
})
class TestModule {
Expand Down