feat(router): add support for route links with no leading slash

Closes #4623
This commit is contained in:
Brian Ford
2015-10-26 17:18:08 +00:00
parent 7af27f9617
commit 07cdc2ff44
4 changed files with 116 additions and 15 deletions

View File

@ -254,6 +254,14 @@ export class RouteRegistry {
return instruction;
}
public hasRoute(name: string, parentComponent: any): boolean {
var componentRecognizer: RouteRecognizer = this._rules.get(parentComponent);
if (isBlank(componentRecognizer)) {
return false;
}
return componentRecognizer.hasRoute(name);
}
// if the child includes a redirect like : "/" -> "/something",
// we want to honor that redirection when creating the link
private _generateRedirects(componentCursor: Type): Instruction {

View File

@ -432,8 +432,21 @@ export class Router {
}
}
} else if (first != '.') {
throw new BaseException(
`Link "${ListWrapper.toJSON(linkParams)}" must start with "/", "./", or "../"`);
// For a link with no leading `./`, `/`, or `../`, we look for a sibling and child.
// If both exist, we throw. Otherwise, we prefer whichever exists.
var childRouteExists = this.registry.hasRoute(first, this.hostComponent);
var parentRouteExists =
isPresent(this.parent) && this.registry.hasRoute(first, this.parent.hostComponent);
if (parentRouteExists && childRouteExists) {
let msg =
`Link "${ListWrapper.toJSON(linkParams)}" is ambiguous, use "./" or "../" to disambiguate.`;
throw new BaseException(msg);
}
if (parentRouteExists) {
router = this.parent;
}
rest = linkParams;
}
if (rest[rest.length - 1] == '') {
@ -445,7 +458,7 @@ export class Router {
throw new BaseException(msg);
}
// TODO: structural cloning and whatnot
var nextInstruction = this.registry.generate(rest, router.hostComponent);
var url = [];
var parent = router.parent;
@ -454,8 +467,6 @@ export class Router {
parent = parent.parent;
}
var nextInstruction = this.registry.generate(rest, router.hostComponent);
while (url.length > 0) {
nextInstruction = url.pop().replaceChild(nextInstruction);
}