Skip to content

Commit 46ad355

Browse files
committed
fix(router): infer top-level routing from app component
Closes angular#1600
1 parent 4965226 commit 46ad355

File tree

6 files changed

+80
-57
lines changed

6 files changed

+80
-57
lines changed
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import {CONST} from 'angular2/src/facade/lang';
2-
import {List} from 'angular2/src/facade/collection';
2+
import {List, Map} from 'angular2/src/facade/collection';
33

44
/**
5-
* You use the RouteConfig annotation to ...
5+
* You use the RouteConfig annotation to add routes to a component.
6+
*
7+
* Supported keys:
8+
* - `path` (required)
9+
* - `component` or `components` (requires exactly one of these)
10+
* - `as` (optional)
611
*/
712
export class RouteConfig {
8-
configs;
13+
configs:List<Map>;
914
@CONST()
10-
constructor(configs:List) {
15+
constructor(configs:List<Map>) {
1116
this.configs = configs;
1217
}
1318
}

modules/angular2/src/router/route_registry.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import {RouteRecognizer} from './route_recognizer';
22
import {Instruction, noopInstruction} from './instruction';
33
import {List, ListWrapper, Map, MapWrapper, StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
4-
import {isPresent, isBlank, isType, StringWrapper, CONST} from 'angular2/src/facade/lang';
4+
import {isPresent, isBlank, isType, StringWrapper, BaseException} from 'angular2/src/facade/lang';
55
import {RouteConfig} from './route_config';
66
import {reflector} from 'angular2/src/reflection/reflection';
77

8-
export const rootHostComponent = 'ROOT_HOST';
9-
108
export class RouteRegistry {
119
_rules:Map<any, RouteRecognizer>;
1210

@@ -28,13 +26,13 @@ export class RouteRegistry {
2826

2927
var components = StringMapWrapper.get(config, 'components');
3028
StringMapWrapper.forEach(components, (component, _) => {
31-
this._configFromComponent(component);
29+
this.configFromComponent(component);
3230
});
3331

3432
recognizer.addConfig(config['path'], config, config['alias']);
3533
}
3634

37-
_configFromComponent(component) {
35+
configFromComponent(component) {
3836
if (!isType(component)) {
3937
return;
4038
}
@@ -59,9 +57,7 @@ export class RouteRegistry {
5957
}
6058

6159

62-
// TODO: make recognized context a class
63-
// TODO: change parentComponent into parentContext
64-
recognize(url:string, parentComponent = rootHostComponent) {
60+
recognize(url:string, parentComponent) {
6561
var componentRecognizer = MapWrapper.get(this._rules, parentComponent);
6662
if (isBlank(componentRecognizer)) {
6763
return null;
@@ -103,9 +99,9 @@ export class RouteRegistry {
10399
return null;
104100
}
105101

106-
generate(name:string, params:any) {
102+
generate(name:string, params:any, hostComponent) {
107103
//TODO: implement for hierarchical routes
108-
var componentRecognizer = MapWrapper.get(this._rules, rootHostComponent);
104+
var componentRecognizer = MapWrapper.get(this._rules, hostComponent);
109105
if (isPresent(componentRecognizer)) {
110106
return componentRecognizer.generate(name, params);
111107
}
@@ -148,7 +144,7 @@ function normalizeConfig(config:StringMap) {
148144

149145
return newConfig;
150146
} else if (!StringMapWrapper.contains(config, 'components')) {
151-
throw new Error('Config does not include a "component" or "components" key.');
147+
throw new BaseException('Config does not include a "component" or "components" key.');
152148
}
153149
return config;
154150
}

modules/angular2/src/router/router.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {Promise, PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
22
import {Map, MapWrapper, List, ListWrapper} from 'angular2/src/facade/collection';
3-
import {isBlank} from 'angular2/src/facade/lang';
3+
import {isBlank, Type} from 'angular2/src/facade/lang';
44

5-
import {RouteRegistry, rootHostComponent} from './route_registry';
5+
import {RouteRegistry} from './route_registry';
66
import {Pipeline} from './pipeline';
77
import {Instruction} from './instruction';
88
import {RouterOutlet} from './router_outlet';
@@ -42,7 +42,6 @@ export class Router {
4242
this._registry = registry;
4343
this._pipeline = pipeline;
4444
this._subject = new EventEmitter();
45-
//this._location.subscribe((url) => this.navigate(url));
4645
}
4746

4847

@@ -86,10 +85,8 @@ export class Router {
8685
*
8786
*/
8887
config(config:any) {
89-
90-
//TODO: use correct check
9188
if (config instanceof List) {
92-
path.forEach((configObject) => {
89+
config.forEach((configObject) => {
9390
// TODO: this is a hack
9491
this._registry.config(this.hostComponent, configObject);
9592
})
@@ -172,7 +169,7 @@ export class Router {
172169
* Given a URL, returns an instruction representing the component graph
173170
*/
174171
recognize(url:string) {
175-
return this._registry.recognize(url);
172+
return this._registry.recognize(url, this.hostComponent);
176173
}
177174

178175

@@ -192,17 +189,15 @@ export class Router {
192189
* Generate a URL from a component name and optional map of parameters. The URL is relative to the app's base href.
193190
*/
194191
generate(name:string, params:any) {
195-
return this._registry.generate(name, params);
196-
}
197-
198-
static getRoot():Router {
199-
return new RootRouter(new Pipeline(), new Location());
192+
return this._registry.generate(name, params, this.hostComponent);
200193
}
201194
}
202195

203196
export class RootRouter extends Router {
204-
constructor(pipeline:Pipeline, location:Location) {
205-
super(new RouteRegistry(), pipeline, location, null, rootHostComponent);
197+
constructor(registry:RouteRegistry, pipeline:Pipeline, location:Location, hostComponent:Type) {
198+
super(registry, pipeline, location, null, hostComponent);
199+
this._location.subscribe((url) => this.navigate(url));
200+
this._registry.configFromComponent(hostComponent);
206201
this.navigate(location.path());
207202
}
208203
}

modules/angular2/test/router/outlet_spec.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,31 @@ import {Router, RouterOutlet, RouterLink, RouteConfig, RouteParams} from 'angula
2626
import {DOM} from 'angular2/src/dom/dom_adapter';
2727

2828
import {DummyLocation} from 'angular2/src/mock/location_mock';
29+
import {Location} from 'angular2/src/router/location';
30+
import {RouteRegistry} from 'angular2/src/router/route_registry';
31+
import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader';
2932

3033
export function main() {
3134
describe('Outlet Directive', () => {
3235

33-
var ctx, tb, view, router;
36+
var ctx, tb, view, rtr;
3437

35-
beforeEach(inject([TestBed], (testBed) => {
38+
beforeEachBindings(() => [
39+
Pipeline,
40+
RouteRegistry,
41+
DirectiveMetadataReader,
42+
bind(Location).toClass(DummyLocation),
43+
bind(Router).toFactory((registry, pipeline, location) => {
44+
return new RootRouter(registry, pipeline, location, MyComp);
45+
}, [RouteRegistry, Pipeline, Location])
46+
]);
47+
48+
beforeEach(inject([TestBed, Router], (testBed, router) => {
3649
tb = testBed;
3750
ctx = new MyComp();
51+
rtr = router;
3852
}));
3953

40-
beforeEachBindings(() => {
41-
router = new RootRouter(new Pipeline(), new DummyLocation());
42-
return [
43-
bind(Router).toValue(router)
44-
];
45-
});
46-
4754
function compile(template:string = "<router-outlet></router-outlet>") {
4855
tb.overrideView(MyComp, new View({template: ('<div>' + template + '</div>'), directives: [RouterOutlet, RouterLink]}));
4956
return tb.createView(MyComp, {context: ctx}).then((v) => {
@@ -53,8 +60,8 @@ export function main() {
5360

5461
it('should work in a simple case', inject([AsyncTestCompleter], (async) => {
5562
compile()
56-
.then((_) => router.config({'path': '/test', 'component': HelloCmp}))
57-
.then((_) => router.navigate('/test'))
63+
.then((_) => rtr.config({'path': '/test', 'component': HelloCmp}))
64+
.then((_) => rtr.navigate('/test'))
5865
.then((_) => {
5966
view.detectChanges();
6067
expect(view.rootNodes).toHaveText('hello');
@@ -65,13 +72,13 @@ export function main() {
6572

6673
it('should navigate between components with different parameters', inject([AsyncTestCompleter], (async) => {
6774
compile()
68-
.then((_) => router.config({'path': '/user/:name', 'component': UserCmp}))
69-
.then((_) => router.navigate('/user/brian'))
75+
.then((_) => rtr.config({'path': '/user/:name', 'component': UserCmp}))
76+
.then((_) => rtr.navigate('/user/brian'))
7077
.then((_) => {
7178
view.detectChanges();
7279
expect(view.rootNodes).toHaveText('hello brian');
7380
})
74-
.then((_) => router.navigate('/user/igor'))
81+
.then((_) => rtr.navigate('/user/igor'))
7582
.then((_) => {
7683
view.detectChanges();
7784
expect(view.rootNodes).toHaveText('hello igor');
@@ -82,8 +89,8 @@ export function main() {
8289

8390
it('should work with child routers', inject([AsyncTestCompleter], (async) => {
8491
compile('outer { <router-outlet></router-outlet> }')
85-
.then((_) => router.config({'path': '/a', 'component': ParentCmp}))
86-
.then((_) => router.navigate('/a/b'))
92+
.then((_) => rtr.config({'path': '/a', 'component': ParentCmp}))
93+
.then((_) => rtr.navigate('/a/b'))
8794
.then((_) => {
8895
view.detectChanges();
8996
expect(view.rootNodes).toHaveText('outer { inner { hello } }');
@@ -95,8 +102,8 @@ export function main() {
95102
it('should generate link hrefs', inject([AsyncTestCompleter], (async) => {
96103
ctx.name = 'brian';
97104
compile('<a href="hello" router-link="user" [router-params]="{name: name}">{{name}}</a>')
98-
.then((_) => router.config({'path': '/user/:name', 'component': UserCmp, 'alias': 'user'}))
99-
.then((_) => router.navigate('/a/b'))
105+
.then((_) => rtr.config({'path': '/user/:name', 'component': UserCmp, 'alias': 'user'}))
106+
.then((_) => rtr.navigate('/a/b'))
100107
.then((_) => {
101108
view.detectChanges();
102109
expect(view.rootNodes).toHaveText('brian');

modules/angular2/test/router/route_registry_spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import {
66
inject, beforeEach,
77
SpyObject} from 'angular2/test_lib';
88

9-
import {RouteRegistry, rootHostComponent} from 'angular2/src/router/route_registry';
9+
import {RouteRegistry} from 'angular2/src/router/route_registry';
1010
import {RouteConfig} from 'angular2/src/router/route_config';
1111

1212
export function main() {
1313
describe('RouteRegistry', () => {
14-
var registry;
14+
var registry,
15+
rootHostComponent = new Object();
1516

1617
beforeEach(() => {
1718
registry = new RouteRegistry();
@@ -21,15 +22,15 @@ export function main() {
2122
registry.config(rootHostComponent, {'path': '/', 'component': DummyCompA});
2223
registry.config(rootHostComponent, {'path': '/test', 'component': DummyCompB});
2324

24-
var instruction = registry.recognize('/test');
25+
var instruction = registry.recognize('/test', rootHostComponent);
2526

2627
expect(instruction.getChildInstruction('default').component).toBe(DummyCompB);
2728
});
2829

2930
it('should match the full URL recursively', () => {
3031
registry.config(rootHostComponent, {'path': '/first', 'component': DummyParentComp});
3132

32-
var instruction = registry.recognize('/first/second');
33+
var instruction = registry.recognize('/first/second', rootHostComponent);
3334

3435
var parentInstruction = instruction.getChildInstruction('default');
3536
var childInstruction = parentInstruction.getChildInstruction('default');

modules/angular2/test/router/router_spec.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,42 @@ import {
44
proxy,
55
it, iit,
66
ddescribe, expect,
7-
inject, beforeEach,
7+
inject, beforeEach, beforeEachBindings,
88
SpyObject} from 'angular2/test_lib';
99
import {IMPLEMENTS} from 'angular2/src/facade/lang';
1010

1111
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
12-
import {RootRouter} from 'angular2/src/router/router';
12+
import {Router, RootRouter} from 'angular2/src/router/router';
1313
import {Pipeline} from 'angular2/src/router/pipeline';
1414
import {RouterOutlet} from 'angular2/src/router/router_outlet';
1515
import {DummyLocation} from 'angular2/src/mock/location_mock'
16+
import {Location} from 'angular2/src/router/location';
17+
18+
import {RouteRegistry} from 'angular2/src/router/route_registry';
19+
import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader';
20+
21+
import {bind} from 'angular2/di';
1622

1723
export function main() {
1824
describe('Router', () => {
1925
var router,
2026
location;
2127

22-
beforeEach(() => {
23-
location = new DummyLocation();
24-
router = new RootRouter(new Pipeline(), location);
25-
});
28+
beforeEachBindings(() => [
29+
Pipeline,
30+
RouteRegistry,
31+
DirectiveMetadataReader,
32+
bind(Location).toClass(DummyLocation),
33+
bind(Router).toFactory((registry, pipeline, location) => {
34+
return new RootRouter(registry, pipeline, location, AppCmp);
35+
}, [RouteRegistry, Pipeline, Location])
36+
]);
37+
38+
39+
beforeEach(inject([Router, Location], (rtr, loc) => {
40+
router = rtr;
41+
location = loc;
42+
}));
2643

2744

2845
it('should navigate based on the initial URL state', inject([AsyncTestCompleter], (async) => {
@@ -82,3 +99,5 @@ function makeDummyRef() {
8299
ref.spy('deactivate').andCallFake((_) => PromiseWrapper.resolve(true));
83100
return ref;
84101
}
102+
103+
class AppCmp {}

0 commit comments

Comments
 (0)