refactor(router): rename "as" to "name" in RouteConfig

BREAKING CHANGE:

This is a rename to make routing concepts easier to understand.

Before:

```
@RouteConfig([
  { path: '/', component: MyCmp, as: 'Home' }
])
```

After:

```
@RouteConfig([
  { path: '/', component: MyCmp, name: 'Home' }
])
```

Closes #4622

Closes #4896
This commit is contained in:
Vamsi V
2015-10-25 15:00:27 +05:30
committed by Brian Ford
parent cc37d0a7fa
commit 7d83959be5
18 changed files with 158 additions and 110 deletions

View File

@ -1,4 +1,4 @@
import {CONST, Type} from 'angular2/src/core/facade/lang';
import {CONST, Type, isPresent} from 'angular2/src/core/facade/lang';
import {RouteDefinition} from './route_definition';
export {RouteDefinition} from './route_definition';
@ -18,7 +18,7 @@ export class RouteConfig {
* It has the following properties:
* - `path` is a string that uses the route matcher DSL.
* - `component` a component type.
* - `as` is an optional `CamelCase` string representing the name of the route.
* - `name` is an optional `CamelCase` string representing the name of the route.
* - `data` is an optional property of any type representing arbitrary route metadata for the given
* route. It is injectable via {@link RouteData}.
*
@ -27,7 +27,7 @@ export class RouteConfig {
* import {RouteConfig} from 'angular2/router';
*
* @RouteConfig([
* {path: '/home', component: HomeCmp, as: 'HomeCmp' }
* {path: '/home', component: HomeCmp, name: 'HomeCmp' }
* ])
* class MyApp {}
* ```
@ -37,15 +37,15 @@ export class Route implements RouteDefinition {
data: {[key: string]: any};
path: string;
component: Type;
as: string;
name: 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?: {[key: string]: any}}) {
constructor({path, component, name,
data}: {path: string, component: Type, name?: string, data?: {[key: string]: any}}) {
this.path = path;
this.component = component;
this.as = as;
this.name = name;
this.loader = null;
this.redirectTo = null;
this.data = data;
@ -58,7 +58,7 @@ export class Route implements RouteDefinition {
* It takes an object with the following properties:
* - `path` is a string that uses the route matcher DSL.
* - `component` a component type.
* - `as` is an optional `CamelCase` string representing the name of the route.
* - `name` is an optional `CamelCase` string representing the name of the route.
* - `data` is an optional property of any type representing arbitrary route metadata for the given
* route. It is injectable via {@link RouteData}.
*
@ -77,14 +77,14 @@ export class AuxRoute implements RouteDefinition {
data: {[key: string]: any} = null;
path: string;
component: Type;
as: string;
name: 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}) {
constructor({path, component, name}: {path: string, component: Type, name?: string}) {
this.path = path;
this.component = component;
this.as = as;
this.name = name;
}
}
@ -95,7 +95,7 @@ export class AuxRoute implements RouteDefinition {
* It has the following properties:
* - `path` is a string that uses the route matcher DSL.
* - `loader` is a function that returns a promise that resolves to a component.
* - `as` is an optional `CamelCase` string representing the name of the route.
* - `name` is an optional `CamelCase` string representing the name of the route.
* - `data` is an optional property of any type representing arbitrary route metadata for the given
* route. It is injectable via {@link RouteData}.
*
@ -104,7 +104,7 @@ export class AuxRoute implements RouteDefinition {
* import {RouteConfig} from 'angular2/router';
*
* @RouteConfig([
* {path: '/home', loader: () => Promise.resolve(MyLoadedCmp), as: 'MyLoadedCmp'}
* {path: '/home', loader: () => Promise.resolve(MyLoadedCmp), name: 'MyLoadedCmp'}
* ])
* class MyApp {}
* ```
@ -114,12 +114,12 @@ export class AsyncRoute implements RouteDefinition {
data: {[key: string]: any};
path: string;
loader: Function;
as: string;
constructor({path, loader, as,
data}: {path: string, loader: Function, as?: string, data?: {[key: string]: any}}) {
name: string;
constructor({path, loader, name, data}:
{path: string, loader: Function, name?: string, data?: {[key: string]: any}}) {
this.path = path;
this.loader = loader;
this.as = as;
this.name = name;
this.data = data;
}
}
@ -147,7 +147,7 @@ export class AsyncRoute implements RouteDefinition {
export class Redirect implements RouteDefinition {
path: string;
redirectTo: string;
as: string = null;
name: string = null;
// added next property to work around https://github.com/Microsoft/TypeScript/issues/4107
loader: Function = null;
data: any = null;

View File

@ -17,8 +17,14 @@ export function normalizeRouteConfig(config: RouteDefinition): RouteDefinition {
throw new BaseException(
`Route config should contain exactly one "component", "loader", or "redirectTo" property.`);
}
if (config.as && config.name) {
throw new BaseException(`Route config should contain exactly one "as" or "name" property.`);
}
if (config.as) {
config.name = config.as;
}
if (config.loader) {
return new AsyncRoute({path: config.path, loader: config.loader, as: config.as});
return new AsyncRoute({path: config.path, loader: config.loader, name: config.name});
}
if (config.component) {
if (typeof config.component == 'object') {
@ -27,11 +33,11 @@ export function normalizeRouteConfig(config: RouteDefinition): RouteDefinition {
return new Route({
path: config.path,
component:<Type>componentDefinitionObject.constructor,
as: config.as
name: config.name
});
} else if (componentDefinitionObject.type == 'loader') {
return new AsyncRoute(
{path: config.path, loader: componentDefinitionObject.loader, as: config.as});
{path: config.path, loader: componentDefinitionObject.loader, name: config.name});
} else {
throw new BaseException(
`Invalid component type "${componentDefinitionObject.type}". Valid types are "constructor" and "loader".`);
@ -40,7 +46,7 @@ export function normalizeRouteConfig(config: RouteDefinition): RouteDefinition {
return new Route(<{
path: string;
component: Type;
as?: string
name?: string;
}>config);
}

View File

@ -2,6 +2,6 @@ library angular2.src.router.route_definition;
abstract class RouteDefinition {
final String path;
final String as;
const RouteDefinition({this.path, this.as});
final String name;
const RouteDefinition({this.path, this.name});
}

View File

@ -6,7 +6,7 @@ import {CONST, Type} from 'angular2/src/core/facade/lang';
* Supported keys:
* - `path` (required)
* - `component`, `loader`, `redirectTo` (requires exactly one of these)
* - `as` (optional)
* - `name` or `as` (optional) (requires exactly one of these)
* - `data` (optional)
*
* See also {@link Route}, {@link AsyncRoute}, {@link AuxRoute}, and {@link Redirect}.
@ -17,6 +17,7 @@ export interface RouteDefinition {
loader?: Function;
redirectTo?: string;
as?: string;
name?: string;
data?: any;
}

View File

@ -38,10 +38,10 @@ export class RouteRecognizer {
config(config: RouteDefinition): boolean {
var handler;
if (isPresent(config.as) && config.as[0].toUpperCase() != config.as[0]) {
var suggestedAlias = config.as[0].toUpperCase() + config.as.substring(1);
if (isPresent(config.name) && config.name[0].toUpperCase() != config.name[0]) {
var suggestedName = config.name[0].toUpperCase() + config.name.substring(1);
throw new BaseException(
`Route '${config.path}' with alias '${config.as}' does not begin with an uppercase letter. Route aliases should be CamelCase like '${suggestedAlias}'.`);
`Route "${config.path}" with name "${config.name}" does not begin with an uppercase letter. Route names should be CamelCase like "${suggestedName}".`);
}
if (config instanceof AuxRoute) {
@ -72,8 +72,8 @@ export class RouteRecognizer {
});
this.matchers.push(recognizer);
if (isPresent(config.as)) {
this.names.set(config.as, recognizer);
if (isPresent(config.name)) {
this.names.set(config.name, recognizer);
}
return recognizer.terminal;
}