feat(router): add support for wildcards

This commit is contained in:
vsavkin 2016-04-30 18:08:26 -07:00 committed by Victor Savkin
parent 6f5e3f9390
commit 8836219b16
2 changed files with 30 additions and 3 deletions

View File

@ -47,9 +47,12 @@ function _constructSegment(componentResolver: ComponentResolver,
matched: _MatchResult): Promise<TreeNode<RouteSegment>[]> { matched: _MatchResult): Promise<TreeNode<RouteSegment>[]> {
return componentResolver.resolveComponent(matched.component) return componentResolver.resolveComponent(matched.component)
.then(factory => { .then(factory => {
let urlOutlet = matched.consumedUrlSegments[0].outlet; let urlOutlet = matched.consumedUrlSegments.length === 0 ||
let segment = new RouteSegment(matched.consumedUrlSegments, matched.parameters, isBlank(matched.consumedUrlSegments[0].outlet) ?
isBlank(urlOutlet) ? DEFAULT_OUTLET_NAME : urlOutlet, DEFAULT_OUTLET_NAME :
matched.consumedUrlSegments[0].outlet;
let segment = new RouteSegment(matched.consumedUrlSegments, matched.parameters, urlOutlet,
matched.component, factory); matched.component, factory);
if (matched.leftOverUrl.length > 0) { if (matched.leftOverUrl.length > 0) {
@ -102,6 +105,11 @@ function _match(metadata: RoutesMetadata, url: TreeNode<UrlSegment>): _MatchResu
function _matchWithParts(route: RouteMetadata, url: TreeNode<UrlSegment>): _MatchResult { function _matchWithParts(route: RouteMetadata, url: TreeNode<UrlSegment>): _MatchResult {
let path = route.path.startsWith("/") ? route.path.substring(1) : route.path; let path = route.path.startsWith("/") ? route.path.substring(1) : route.path;
if (path == "*") {
return new _MatchResult(route.component, [], null, [], []);
}
let parts = path.split("/"); let parts = path.split("/");
let positionalParams = {}; let positionalParams = {};
let consumedUrlSegments = []; let consumedUrlSegments = [];

View File

@ -149,6 +149,19 @@ export function main() {
}); });
})); }));
it('should match a wildcard',
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
recognize(resolver, ComponentG, tree("a;aa=1/b;bb=2"))
.then(r => {
let c = r.children(r.root);
expect(c.length).toEqual(1);
expect(stringifyUrl(c[0].urlSegments)).toEqual([]);
expect(c[0].parameters).toEqual(null);
async.done();
});
}));
it('should error when no matching routes', it('should error when no matching routes',
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
recognize(resolver, ComponentA, tree("invalid")) recognize(resolver, ComponentA, tree("invalid"))
@ -214,6 +227,12 @@ class ComponentC {
class ComponentB { class ComponentB {
} }
@Component({selector: 'g', template: 't'})
@Routes(
[new Route({path: "d", component: ComponentD}), new Route({path: "*", component: ComponentE})])
class ComponentG {
}
@Component({selector: 'a', template: 't'}) @Component({selector: 'a', template: 't'})
@Routes([ @Routes([
new Route({path: "b/:b", component: ComponentB}), new Route({path: "b/:b", component: ComponentB}),