fix(RouterLink): ignore optional parameters when checking for active routes

fixes #6459
Closes #7834
This commit is contained in:
Victor Berchet 2016-03-30 11:26:31 -07:00
parent 28e657d857
commit 5e2bc5c593
6 changed files with 32 additions and 10 deletions

View File

@ -310,7 +310,7 @@ export class ComponentInstruction {
*/ */
constructor(public urlPath: string, public urlParams: string[], data: RouteData, constructor(public urlPath: string, public urlParams: string[], data: RouteData,
public componentType, public terminal: boolean, public specificity: string, public componentType, public terminal: boolean, public specificity: string,
public params: {[key: string]: string} = null) { public params: {[key: string]: string} = null, public routeName: string) {
this.routeData = isPresent(data) ? data : BLANK_ROUTE_DATA; this.routeData = isPresent(data) ? data : BLANK_ROUTE_DATA;
} }
} }

View File

@ -135,12 +135,33 @@ export class Router {
*/ */
isRouteActive(instruction: Instruction): boolean { isRouteActive(instruction: Instruction): boolean {
var router: Router = this; var router: Router = this;
if (isBlank(this.currentInstruction)) {
return false;
}
// `instruction` corresponds to the root router
while (isPresent(router.parent) && isPresent(instruction.child)) { while (isPresent(router.parent) && isPresent(instruction.child)) {
router = router.parent; router = router.parent;
instruction = instruction.child; instruction = instruction.child;
} }
return isPresent(this.currentInstruction) &&
this.currentInstruction.component == instruction.component; if (isBlank(instruction.component) || isBlank(this.currentInstruction.component) ||
this.currentInstruction.component.routeName != instruction.component.routeName) {
return false;
}
let paramEquals = true;
if (isPresent(this.currentInstruction.component.params)) {
StringMapWrapper.forEach(instruction.component.params, (value, key) => {
if (this.currentInstruction.component.params[key] !== value) {
paramEquals = false;
}
});
}
return paramEquals;
} }

View File

@ -59,7 +59,7 @@ export class RuleSet {
if (config instanceof AuxRoute) { if (config instanceof AuxRoute) {
handler = new SyncRouteHandler(config.component, config.data); handler = new SyncRouteHandler(config.component, config.data);
let routePath = this._getRoutePath(config); let routePath = this._getRoutePath(config);
let auxRule = new RouteRule(routePath, handler); let auxRule = new RouteRule(routePath, handler, config.name);
this.auxRulesByPath.set(routePath.toString(), auxRule); this.auxRulesByPath.set(routePath.toString(), auxRule);
if (isPresent(config.name)) { if (isPresent(config.name)) {
this.auxRulesByName.set(config.name, auxRule); this.auxRulesByName.set(config.name, auxRule);
@ -85,7 +85,7 @@ export class RuleSet {
useAsDefault = isPresent(config.useAsDefault) && config.useAsDefault; useAsDefault = isPresent(config.useAsDefault) && config.useAsDefault;
} }
let routePath = this._getRoutePath(config); let routePath = this._getRoutePath(config);
let newRule = new RouteRule(routePath, handler); let newRule = new RouteRule(routePath, handler, config.name);
this._assertNoHashCollision(newRule.hash, config.path); this._assertNoHashCollision(newRule.hash, config.path);

View File

@ -69,7 +69,8 @@ export class RouteRule implements AbstractRule {
// TODO: cache component instruction instances by params and by ParsedUrl instance // TODO: cache component instruction instances by params and by ParsedUrl instance
constructor(private _routePath: RoutePath, public handler: RouteHandler) { constructor(private _routePath: RoutePath, public handler: RouteHandler,
private _routeName: string) {
this.specificity = this._routePath.specificity; this.specificity = this._routePath.specificity;
this.hash = this._routePath.hash; this.hash = this._routePath.hash;
this.terminal = this._routePath.terminal; this.terminal = this._routePath.terminal;
@ -112,7 +113,7 @@ export class RouteRule implements AbstractRule {
} }
var instruction = var instruction =
new ComponentInstruction(urlPath, urlParams, this.handler.data, this.handler.componentType, new ComponentInstruction(urlPath, urlParams, this.handler.data, this.handler.componentType,
this.terminal, this.specificity, params); this.terminal, this.specificity, params, this._routeName);
this._cache.set(hashKey, instruction); this._cache.set(hashKey, instruction);
return instruction; return instruction;

View File

@ -34,7 +34,7 @@ import {DOM} from 'angular2/src/platform/dom/dom_adapter';
import {ResolvedInstruction} from 'angular2/src/router/instruction'; import {ResolvedInstruction} from 'angular2/src/router/instruction';
let dummyInstruction = new ResolvedInstruction( let dummyInstruction = new ResolvedInstruction(
new ComponentInstruction('detail', [], null, null, true, '0'), null, {}); new ComponentInstruction('detail', [], null, null, true, '0', null, 'Detail'), null, {});
export function main() { export function main() {
describe('routerLink directive', function() { describe('routerLink directive', function() {

View File

@ -259,7 +259,7 @@ export function main() {
async.done(); async.done();
}); });
router.navigateByUrl('/better-child'); router.navigateByUrl('/better-child?extra=0');
}); });
})); }));
@ -300,7 +300,7 @@ export function main() {
async.done(); async.done();
}); });
router.navigateByUrl('/child-with-grandchild/grandchild'); router.navigateByUrl('/child-with-grandchild/grandchild?extra=0');
}); });
})); }));