Skip to content

Commit

Permalink
feat(router): add option to compare query params
Browse files Browse the repository at this point in the history
The router would not reload the view if query parameters changed in the same route. We add this behavior to navigation-plan.js and we add the `compareQueryParams` option in order to opt in to this behavior.

Fixes #268.
  • Loading branch information
jwahyoung committed Mar 10, 2016
1 parent 47e77de commit 37ab9fa
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/navigation-instruction.js
Expand Up @@ -6,7 +6,8 @@ interface NavigationInstructionInit {
config: RouteConfig,
parentInstruction: NavigationInstruction,
previousInstruction: NavigationInstruction,
router: Router
router: Router,
options: Object
}

export class CommitChangesStep {
Expand Down Expand Up @@ -64,6 +65,8 @@ export class NavigationInstruction {

plan: Object = null;

options: Object = {};

constructor(init: NavigationInstructionInit) {
Object.assign(this, init);

Expand Down
18 changes: 18 additions & 0 deletions src/navigation-plan.js
Expand Up @@ -121,6 +121,24 @@ function hasDifferentParameterValues(prev: NavigationInstruction, next: Navigati
}
}

if (!next.options.compareQueryParams) {
return false;
}

let prevQueryParams = prev.queryParams;
let nextQueryParams = next.queryParams;
for (let key in nextQueryParams) {
if (prevQueryParams[key] !== nextQueryParams[key]) {
return true;
}
}

for (let key in prevQueryParams) {
if (prevQueryParams[key] !== nextQueryParams[key]) {
return true;
}
}

return false;
}

Expand Down
7 changes: 6 additions & 1 deletion src/router.js
Expand Up @@ -51,6 +51,8 @@ export class Router {
*/
parent: Router = null;

options: Object = {};

/**
* @param container The [[Container]] to use when child routers.
* @param history The [[History]] implementation to delegate navigation requests to.
Expand Down Expand Up @@ -369,7 +371,10 @@ export class Router {
config: null,
parentInstruction,
previousInstruction: this.currentInstruction,
router: this
router: this,
options: {
compareQueryParams: this.options.compareQueryParams
}
};

if (results && results.length) {
Expand Down
14 changes: 14 additions & 0 deletions test/navigation-plan.spec.js
Expand Up @@ -141,5 +141,19 @@ describe('NavigationPlanStep', () => {
done();
});
});

it('is invoke-lifecycle when query params change and ignoreQueryParams is false', (done) => {
firstInstruction.queryParams = { param: 'foo' };
sameAsFirstInstruction.queryParams = { param: 'bar' };
sameAsFirstInstruction.options.compareQueryParams = true;
firstInstruction.addViewPortInstruction('default', 'ignored', './first', { viewModel: {}});

step.run(sameAsFirstInstruction, state.next)
.then(() => {
expect(state.result).toBe(true);
expect(sameAsFirstInstruction.plan.default.strategy).toBe('invoke-lifecycle');
done();
});
});
});
});

0 comments on commit 37ab9fa

Please sign in to comment.