From 9153331303253a8ad71cf4ce581795686c401694 Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Sun, 3 May 2015 20:25:26 -0700 Subject: [PATCH] feat(router): route redirects --- modules/angular2/src/router/route_config.js | 2 +- modules/angular2/src/router/route_registry.js | 13 ++++++++----- modules/angular2/test/router/outlet_spec.js | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/modules/angular2/src/router/route_config.js b/modules/angular2/src/router/route_config.js index 688a843123..505a9b912f 100644 --- a/modules/angular2/src/router/route_config.js +++ b/modules/angular2/src/router/route_config.js @@ -6,7 +6,7 @@ import {List, Map} from 'angular2/src/facade/collection'; * * Supported keys: * - `path` (required) - * - `component` or `components` (requires exactly one of these) + * - `component`, `components`, `redirectTo` (requires exactly one of these) * - `as` (optional) */ export class RouteConfig { diff --git a/modules/angular2/src/router/route_registry.js b/modules/angular2/src/router/route_registry.js index 5b7d9b8330..812aec789c 100644 --- a/modules/angular2/src/router/route_registry.js +++ b/modules/angular2/src/router/route_registry.js @@ -13,14 +13,14 @@ export class RouteRegistry { } config(parentComponent, config) { - if (!StringMapWrapper.contains(config, 'path')) { throw new BaseException('Route config does not contain "path"'); } if (!StringMapWrapper.contains(config, 'component') && - !StringMapWrapper.contains(config, 'components')) { - throw new BaseException('Route config does not contain "component" or "components"'); + !StringMapWrapper.contains(config, 'components') && + !StringMapWrapper.contains(config, 'redirectTo')) { + throw new BaseException('Route config does not contain "component," "components," or "redirectTo"'); } var recognizer:RouteRecognizer; @@ -33,6 +33,11 @@ export class RouteRegistry { config = normalizeConfig(config); + if (StringMapWrapper.contains(config, 'redirectTo')) { + recognizer.addRedirect(StringMapWrapper.get(config, 'path'), StringMapWrapper.get(config, 'redirectTo')); + return; + } + var components = StringMapWrapper.get(config, 'components'); StringMapWrapper.forEach(components, (component, _) => { this.configFromComponent(component); @@ -152,8 +157,6 @@ function normalizeConfig(config:StringMap) { }); return newConfig; - } else if (!StringMapWrapper.contains(config, 'components')) { - throw new BaseException('Config does not include a "component" or "components" key.'); } return config; } diff --git a/modules/angular2/test/router/outlet_spec.js b/modules/angular2/test/router/outlet_spec.js index 6960c78770..499c8aedd8 100644 --- a/modules/angular2/test/router/outlet_spec.js +++ b/modules/angular2/test/router/outlet_spec.js @@ -117,6 +117,20 @@ export function main() { })); + it('should work with redirects', inject([AsyncTestCompleter, Location], (async, location) => { + compile() + .then((_) => rtr.config({'path': '/original', 'redirectTo': '/redirected' })) + .then((_) => rtr.config({'path': '/redirected', 'component': A })) + .then((_) => rtr.navigate('/original')) + .then((_) => { + view.detectChanges(); + expect(view.rootNodes).toHaveText('A'); + expect(location.urlChanges).toEqual(['/redirected']); + async.done(); + }); + })); + + it('should generate link hrefs', inject([AsyncTestCompleter], (async) => { ctx.name = 'brian'; compile('{{name}}')