X Tutup
Skip to content

Commit 0ebe283

Browse files
committed
feat(router): provide RouteConfig object for AuxRoute
Closes #4319
1 parent 23784a2 commit 0ebe283

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

modules/angular2/src/router/route_config_impl.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,15 @@ export class Route implements RouteDefinition {
3838
path: string;
3939
component: Type;
4040
name: string;
41-
// added next two properties to work around https://github.com/Microsoft/TypeScript/issues/4107
42-
loader: Function;
43-
redirectTo: string;
41+
// added next three properties to work around https://github.com/Microsoft/TypeScript/issues/4107
42+
aux: string = null;
43+
loader: Function = null;
44+
redirectTo: string = null;
4445
constructor({path, component, name,
4546
data}: {path: string, component: Type, name?: string, data?: {[key: string]: any}}) {
4647
this.path = path;
4748
this.component = component;
4849
this.name = name;
49-
this.loader = null;
50-
this.redirectTo = null;
5150
this.data = data;
5251
}
5352
}
@@ -78,7 +77,8 @@ export class AuxRoute implements RouteDefinition {
7877
path: string;
7978
component: Type;
8079
name: string;
81-
// added next two properties to work around https://github.com/Microsoft/TypeScript/issues/4107
80+
// added next three properties to work around https://github.com/Microsoft/TypeScript/issues/4107
81+
aux: string = null;
8282
loader: Function = null;
8383
redirectTo: string = null;
8484
constructor({path, component, name}: {path: string, component: Type, name?: string}) {
@@ -115,6 +115,7 @@ export class AsyncRoute implements RouteDefinition {
115115
path: string;
116116
loader: Function;
117117
name: string;
118+
aux: string = null;
118119
constructor({path, loader, name, data}:
119120
{path: string, loader: Function, name?: string, data?: {[key: string]: any}}) {
120121
this.path = path;
@@ -148,9 +149,10 @@ export class Redirect implements RouteDefinition {
148149
path: string;
149150
redirectTo: string;
150151
name: string = null;
151-
// added next property to work around https://github.com/Microsoft/TypeScript/issues/4107
152+
// added next three properties to work around https://github.com/Microsoft/TypeScript/issues/4107
152153
loader: Function = null;
153154
data: any = null;
155+
aux: string = null;
154156
constructor({path, redirectTo}: {path: string, redirectTo: string}) {
155157
this.path = path;
156158
this.redirectTo = redirectTo;

modules/angular2/src/router/route_config_nomalizer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export function normalizeRouteConfig(config: RouteDefinition): RouteDefinition {
2626
if (config.loader) {
2727
return new AsyncRoute({path: config.path, loader: config.loader, name: config.name});
2828
}
29+
if (config.aux) {
30+
return new AuxRoute({path: config.aux, component:<Type>config.component, name: config.name});
31+
}
2932
if (config.component) {
3033
if (typeof config.component == 'object') {
3134
let componentDefinitionObject = <ComponentDefinition>config.component;

modules/angular2/src/router/route_definition.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import {CONST, Type} from 'angular2/src/core/facade/lang';
44
* `RouteDefinition` defines a route within a {@link RouteConfig} decorator.
55
*
66
* Supported keys:
7-
* - `path` (required)
7+
* - `path` or `aux` (requires exactly one of these)
88
* - `component`, `loader`, `redirectTo` (requires exactly one of these)
99
* - `name` or `as` (optional) (requires exactly one of these)
1010
* - `data` (optional)
1111
*
1212
* See also {@link Route}, {@link AsyncRoute}, {@link AuxRoute}, and {@link Redirect}.
1313
*/
1414
export interface RouteDefinition {
15-
path: string;
15+
path?: string;
16+
aux?: string;
1617
component?: Type | ComponentDefinition;
1718
loader?: Function;
1819
redirectTo?: string;

modules/angular2/src/router/router.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,11 @@ export class Router {
337337
}
338338

339339
var promises = [];
340-
this._auxRouters.forEach(
341-
(router, name) => { promises.push(router.commit(instruction.auxInstruction[name])); });
340+
this._auxRouters.forEach((router, name) => {
341+
if (isPresent(instruction.auxInstruction[name])) {
342+
promises.push(router.commit(instruction.auxInstruction[name]));
343+
}
344+
});
342345

343346
return next.then((_) => PromiseWrapper.all(promises));
344347
}

modules/angular2/test/router/route_config_spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ export function main() {
9797
}));
9898

9999

100+
it('should work in an app with aux routes', inject([AsyncTestCompleter], (async) => {
101+
bootstrap(AuxAppCmp, testBindings)
102+
.then((applicationRef) => {
103+
var router = applicationRef.hostComponent.router;
104+
router.subscribe((_) => {
105+
expect(el).toHaveText('root { hello } aside { hello }');
106+
expect(applicationRef.hostComponent.location.path()).toEqual('/hello(aside)');
107+
async.done();
108+
});
109+
router.navigateByUrl('/hello(aside)');
110+
});
111+
}));
112+
113+
100114
it('should work in an app with async components defined with "loader"',
101115
inject([AsyncTestCompleter], (async) => {
102116
bootstrap(ConciseAsyncAppCmp, testBindings)
@@ -227,6 +241,13 @@ class ConciseAsyncAppCmp {
227241
constructor(public router: Router, public location: LocationStrategy) {}
228242
}
229243

244+
@Component({selector: 'app-cmp'})
245+
@View({template: `root { <router-outlet></router-outlet> } aside { <router-outlet name="aside"></router-outlet> }`, directives: ROUTER_DIRECTIVES})
246+
@RouteConfig([{path: '/hello', component: HelloCmp}, {aux: 'aside', component: HelloCmp}])
247+
class AuxAppCmp {
248+
constructor(public router: Router, public location: LocationStrategy) {}
249+
}
250+
230251
@Component({selector: 'app-cmp'})
231252
@View({template: `root { <router-outlet></router-outlet> }`, directives: ROUTER_DIRECTIVES})
232253
@RouteConfig([

0 commit comments

Comments
 (0)
X Tutup