angular-cn/modules/angular2/src/router/route_config_impl.ts
Daniel Rasmuson ed81cb94b0 feat(router): user metadata in route configs
Provide the ability to attach custom data onto a route and retrieve
that data as an injectable (RouteData) inside the component.

Closes #2777

Closes #3541
2015-08-18 10:33:19 -07:00

89 lines
2.3 KiB
TypeScript

import {CONST, CONST_EXPR, Type} from 'angular2/src/facade/lang';
import {List} from 'angular2/src/facade/collection';
import {RouteDefinition} from './route_definition';
export {RouteDefinition} from './route_definition';
import {OpaqueToken} from 'angular2/di';
export const ROUTE_DATA: OpaqueToken = CONST_EXPR(new OpaqueToken('routeData'));
/**
* You use the RouteConfig annotation to add routes to a component.
*
* Supported keys:
* - `path` (required)
* - `component`, `loader`, `redirectTo` (requires exactly one of these)
* - `as` (optional)
* - `data` (optional)
*/
@CONST()
export class RouteConfig {
constructor(public configs: List<RouteDefinition>) {}
}
@CONST()
export class Route implements RouteDefinition {
data: any;
path: string;
component: Type;
as: string;
// added next two properties to work around https://github.com/Microsoft/TypeScript/issues/4107
loader: Function;
redirectTo: string;
constructor({path, component, as, data}:
{path: string, component: Type, as?: string, data?: any}) {
this.path = path;
this.component = component;
this.as = as;
this.loader = null;
this.redirectTo = null;
this.data = data;
}
}
@CONST()
export class AuxRoute implements RouteDefinition {
data: any = null;
path: string;
component: Type;
as: string;
// added next two properties to work around https://github.com/Microsoft/TypeScript/issues/4107
loader: Function = null;
redirectTo: string = null;
constructor({path, component, as}: {path: string, component: Type, as?: string}) {
this.path = path;
this.component = component;
this.as = as;
}
}
@CONST()
export class AsyncRoute implements RouteDefinition {
data: any;
path: string;
loader: Function;
as: string;
constructor({path, loader, as, data}: {path: string, loader: Function, as?: string, data?: any}) {
this.path = path;
this.loader = loader;
this.as = as;
this.data = data;
}
}
@CONST()
export class Redirect implements RouteDefinition {
path: string;
redirectTo: string;
as: string = null;
// added next property to work around https://github.com/Microsoft/TypeScript/issues/4107
loader: Function = null;
data: any = null;
constructor({path, redirectTo}: {path: string, redirectTo: string}) {
this.path = path;
this.redirectTo = redirectTo;
}
}