Skip to content

Commit 791caf0

Browse files
committed
fix(router): use appRootComponentToken to get root route configs
Closes angular#1947
1 parent 8ab7735 commit 791caf0

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

modules/angular2/router.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import {Router, RootRouter} from './src/router/router';
1818
import {RouteRegistry} from './src/router/route_registry';
1919
import {Pipeline} from './src/router/pipeline';
2020
import {Location} from './src/router/location';
21-
import {appComponentRefToken} from './src/core/application_tokens';
21+
import {appComponentTypeToken} from './src/core/application_tokens';
2222
import {bind} from './di';
2323

2424
export var routerInjectables:List = [
2525
RouteRegistry,
2626
Pipeline,
2727
BrowserLocation,
2828
Location,
29-
bind(Router).toFactory((registry, pipeline, location, app) => {
30-
return new RootRouter(registry, pipeline, location, app.hostComponentType);
31-
}, [RouteRegistry, Pipeline, Location, appComponentRefToken])
29+
bind(Router).toFactory((registry, pipeline, location, appRoot) => {
30+
return new RootRouter(registry, pipeline, location, appRoot);
31+
}, [RouteRegistry, Pipeline, Location, appComponentTypeToken])
3232
];

modules/angular2/src/core/application.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import {DefaultDomCompiler} from 'angular2/src/render/dom/compiler/compiler';
3939
import {internalView} from 'angular2/src/core/compiler/view_ref';
4040

4141
import {
42-
appComponentRefToken
42+
appComponentRefToken,
43+
appComponentTypeToken
4344
} from './application_tokens';
4445

4546
var _rootInjector: Injector;
@@ -53,6 +54,7 @@ var _rootBindings = [
5354
function _injectorBindings(appComponentType): List<Binding> {
5455
return [
5556
bind(DOCUMENT_TOKEN).toValue(DOM.defaultDoc()),
57+
bind(appComponentTypeToken).toValue(appComponentType),
5658
bind(appComponentRefToken).toAsyncFactory((dynamicComponentLoader, injector,
5759
testability, registry) => {
5860

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import {OpaqueToken} from 'angular2/di';
22

33
export var appComponentRefToken:OpaqueToken = new OpaqueToken('ComponentRef');
4+
export var appComponentTypeToken:OpaqueToken = new OpaqueToken('RootComponent');

modules/angular2/src/router/router_outlet.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class RouterOutlet {
3737
_loader:DynamicComponentLoader;
3838
_componentRef:ComponentRef;
3939
_elementRef:ElementRef;
40+
_currentInstruction:Instruction;
4041

4142
constructor(elementRef:ElementRef, loader:DynamicComponentLoader, router:routerMod.Router, injector:Injector, @Attribute('name') nameAttr:String) {
4243
if (isBlank(nameAttr)) {
@@ -49,6 +50,7 @@ export class RouterOutlet {
4950

5051
this._childRouter = null;
5152
this._componentRef = null;
53+
this._currentInstruction = null;
5254
this._parentRouter.registerOutlet(this, nameAttr);
5355
}
5456

@@ -58,10 +60,11 @@ export class RouterOutlet {
5860
activate(instruction:Instruction): Promise {
5961
// if we're able to reuse the component, we just have to pass along the instruction to the component's router
6062
// so it can propagate changes to its children
61-
if (instruction.reuse && isPresent(this._childRouter)) {
63+
if ((instruction == this._currentInstruction) || instruction.reuse && isPresent(this._childRouter)) {
6264
return this._childRouter.commit(instruction);
6365
}
6466

67+
this._currentInstruction = instruction;
6568
this._childRouter = this._parentRouter.childRouter(instruction.component);
6669
var outletInjector = this._injector.resolveAndCreateChild([
6770
bind(RouteParams).toValue(new RouteParams(instruction.params)),
@@ -71,6 +74,7 @@ export class RouterOutlet {
7174
if (isPresent(this._componentRef)) {
7275
this._componentRef.dispose();
7376
}
77+
7478
return this._loader.loadNextToExistingLocation(instruction.component, this._elementRef, outletInjector).then((componentRef) => {
7579
this._componentRef = componentRef;
7680
return this._childRouter.commit(instruction);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
AsyncTestCompleter,
3+
beforeEach,
4+
ddescribe,
5+
describe,
6+
expect,
7+
iit,
8+
inject,
9+
it,
10+
xdescribe,
11+
xit,
12+
} from 'angular2/test_lib';
13+
14+
import {bootstrap} from 'angular2/src/core/application';
15+
import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations';
16+
import {DOM} from 'angular2/src/dom/dom_adapter';
17+
import {bind} from 'angular2/di';
18+
import {View} from 'angular2/src/core/annotations_impl/view';
19+
import {DOCUMENT_TOKEN} from 'angular2/src/render/dom/dom_renderer';
20+
import {RouteConfig} from 'angular2/src/router/route_config_impl';
21+
import {routerInjectables, Router, RouteParams, RouterOutlet} from 'angular2/router';
22+
import {SpyLocation} from 'angular2/src/mock/location_mock';
23+
import {Location} from 'angular2/src/router/location';
24+
25+
export function main() {
26+
describe('router injectables', () => {
27+
var fakeDoc, el, testBindings;
28+
beforeEach(() => {
29+
fakeDoc = DOM.createHtmlDocument();
30+
el = DOM.createElement('app-cmp', fakeDoc);
31+
DOM.appendChild(fakeDoc.body, el);
32+
testBindings = [
33+
routerInjectables,
34+
bind(Location).toClass(SpyLocation),
35+
bind(DOCUMENT_TOKEN).toValue(fakeDoc)
36+
];
37+
});
38+
39+
it('should support bootstrap a simple app', inject([AsyncTestCompleter], (async) => {
40+
bootstrap(AppCmp, testBindings).then((applicationRef) => {
41+
var router = applicationRef.hostComponent.router;
42+
router.subscribe((_) => {
43+
expect(el).toHaveText('outer { hello }');
44+
async.done();
45+
});
46+
});
47+
}));
48+
49+
//TODO: add a test in which the child component has bindings
50+
});
51+
}
52+
53+
54+
@Component({
55+
selector: 'hello-cmp'
56+
})
57+
@View({
58+
template: "hello"
59+
})
60+
class HelloCmp {}
61+
62+
63+
@Component({
64+
selector: 'app-cmp'
65+
})
66+
@View({
67+
template: "outer { <router-outlet></router-outlet> }",
68+
directives: [RouterOutlet]
69+
})
70+
@RouteConfig([{
71+
path: '/', component: HelloCmp
72+
}])
73+
class AppCmp {
74+
router:Router;
75+
constructor(router:Router) {
76+
this.router = router;
77+
}
78+
}

0 commit comments

Comments
 (0)