Skip to content

Commit

Permalink
feat(Router): call resolver when upstream params change (#12942)
Browse files Browse the repository at this point in the history
With this change the resolver is called when the parameter for the activated and any parent routes change.
ie, switching from `/teams/10/players/5` to `/teams/12/players/5` will now trigger any `PlayerResolver`.
  • Loading branch information
elbywan authored and matsko committed Jan 9, 2017
1 parent 46cb04d commit d4d3782
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
8 changes: 6 additions & 2 deletions modules/@angular/router/src/router_state.ts
Expand Up @@ -356,5 +356,9 @@ 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);
}
const equalUrlParams = shallowEqual(a.params, b.params) && equalSegments(a.url, b.url);
const parentsMismatch = !a.parent !== !b.parent;

return equalUrlParams && !parentsMismatch &&
(!a.parent || equalParamsAndUrlSegments(a.parent, b.parent));
}
47 changes: 45 additions & 2 deletions modules/@angular/router/test/router_state.spec.ts
Expand Up @@ -100,9 +100,28 @@ describe('RouterState & Snapshot', () => {

describe('equalParamsAndUrlSegments', () => {
function createSnapshot(params: Params, url: UrlSegment[]): ActivatedRouteSnapshot {
return new ActivatedRouteSnapshot(
const snapshot = new ActivatedRouteSnapshot(
url, params, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null,
-1, null);
snapshot._routerState = new RouterStateSnapshot('', new TreeNode(snapshot, []));
return snapshot;
}

function createSnapshotPairWithParent(
params: [Params, Params], parentParams: [Params, Params],
urls: [string, string]): [ActivatedRouteSnapshot, ActivatedRouteSnapshot] {
const snapshot1 = createSnapshot(params[0], []);
const snapshot2 = createSnapshot(params[1], []);

const snapshot1Parent = createSnapshot(parentParams[0], [new UrlSegment(urls[0], {})]);
const snapshot2Parent = createSnapshot(parentParams[1], [new UrlSegment(urls[1], {})]);

snapshot1._routerState =
new RouterStateSnapshot('', new TreeNode(snapshot1Parent, [new TreeNode(snapshot1, [])]));
snapshot2._routerState =
new RouterStateSnapshot('', new TreeNode(snapshot2Parent, [new TreeNode(snapshot2, [])]));

return [snapshot1, snapshot2];
}

it('should return false when params are different', () => {
Expand All @@ -123,6 +142,27 @@ describe('RouterState & Snapshot', () => {
createSnapshot({a: 1}, [new UrlSegment('a', {})])))
.toEqual(true);
});

it('should return false when upstream params are different', () => {
const [snapshot1, snapshot2] =
createSnapshotPairWithParent([{a: 1}, {a: 1}], [{b: 1}, {c: 1}], ['a', 'a']);

expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(false);
});

it('should return false when upstream urls are different', () => {
const [snapshot1, snapshot2] =
createSnapshotPairWithParent([{a: 1}, {a: 1}], [{b: 1}, {b: 1}], ['a', 'b']);

expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(false);
});

it('should return true when upstream urls and params are equal', () => {
const [snapshot1, snapshot2] =
createSnapshotPairWithParent([{a: 1}, {a: 1}], [{b: 1}, {b: 1}], ['a', 'a']);

expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(true);
});
});

describe('advanceActivatedRoute', () => {
Expand All @@ -135,9 +175,12 @@ describe('RouterState & Snapshot', () => {
const queryParams = {};
const fragment = '';
const data = {};
return new ActivatedRouteSnapshot(
const snapshot = new ActivatedRouteSnapshot(
url, params, queryParams, fragment, data, <any>null, <any>null, <any>null, <any>null, -1,
null);
const state = new RouterStateSnapshot('', new TreeNode(snapshot, []));
snapshot._routerState = state;
return snapshot;
}

it('should call change observers', () => {
Expand Down

1 comment on commit d4d3782

@bfricka
Copy link

@bfricka bfricka commented on d4d3782 Mar 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three cheers!

Please sign in to comment.