feat(router): route redirects

This commit is contained in:
Brian Ford 2015-05-03 20:25:26 -07:00
parent 9d5c33f9dd
commit 9153331303
3 changed files with 23 additions and 6 deletions

View File

@ -6,7 +6,7 @@ import {List, Map} from 'angular2/src/facade/collection';
* *
* Supported keys: * Supported keys:
* - `path` (required) * - `path` (required)
* - `component` or `components` (requires exactly one of these) * - `component`, `components`, `redirectTo` (requires exactly one of these)
* - `as` (optional) * - `as` (optional)
*/ */
export class RouteConfig { export class RouteConfig {

View File

@ -13,14 +13,14 @@ export class RouteRegistry {
} }
config(parentComponent, config) { config(parentComponent, config) {
if (!StringMapWrapper.contains(config, 'path')) { if (!StringMapWrapper.contains(config, 'path')) {
throw new BaseException('Route config does not contain "path"'); throw new BaseException('Route config does not contain "path"');
} }
if (!StringMapWrapper.contains(config, 'component') && if (!StringMapWrapper.contains(config, 'component') &&
!StringMapWrapper.contains(config, 'components')) { !StringMapWrapper.contains(config, 'components') &&
throw new BaseException('Route config does not contain "component" or "components"'); !StringMapWrapper.contains(config, 'redirectTo')) {
throw new BaseException('Route config does not contain "component," "components," or "redirectTo"');
} }
var recognizer:RouteRecognizer; var recognizer:RouteRecognizer;
@ -33,6 +33,11 @@ export class RouteRegistry {
config = normalizeConfig(config); 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'); var components = StringMapWrapper.get(config, 'components');
StringMapWrapper.forEach(components, (component, _) => { StringMapWrapper.forEach(components, (component, _) => {
this.configFromComponent(component); this.configFromComponent(component);
@ -152,8 +157,6 @@ function normalizeConfig(config:StringMap) {
}); });
return newConfig; return newConfig;
} else if (!StringMapWrapper.contains(config, 'components')) {
throw new BaseException('Config does not include a "component" or "components" key.');
} }
return config; return config;
} }

View File

@ -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) => { it('should generate link hrefs', inject([AsyncTestCompleter], (async) => {
ctx.name = 'brian'; ctx.name = 'brian';
compile('<a href="hello" router-link="user" [router-params]="{name: name}">{{name}}</a>') compile('<a href="hello" router-link="user" [router-params]="{name: name}">{{name}}</a>')