Skip to content

Commit 83b97c4

Browse files
committed
refactor(router): use DynamicComponentLoader instead of ViewContainer
1 parent 5db8907 commit 83b97c4

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

modules/angular2/src/router/router_outlet.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {isBlank, isPresent} from 'angular2/src/facade/lang';
33

44
import {Directive} from 'angular2/src/core/annotations_impl/annotations';
55
import {Attribute} from 'angular2/src/core/annotations_impl/di';
6-
import {Compiler, ViewContainerRef} from 'angular2/core';
6+
import {DynamicComponentLoader, ComponentRef, ElementRef} from 'angular2/core';
77
import {Injector, bind} from 'angular2/di';
88

99
import * as routerMod from './router';
@@ -31,48 +31,55 @@ import {Instruction, RouteParams} from './instruction'
3131
selector: 'router-outlet'
3232
})
3333
export class RouterOutlet {
34-
_compiler:Compiler;
3534
_injector:Injector;
3635
_parentRouter:routerMod.Router;
3736
_childRouter:routerMod.Router;
38-
_viewContainer:ViewContainerRef;
37+
_loader:DynamicComponentLoader;
38+
_componentRef:ComponentRef;
39+
_elementRef:ElementRef;
3940

40-
constructor(viewContainer:ViewContainerRef, compiler:Compiler, router:routerMod.Router, injector:Injector, @Attribute('name') nameAttr:String) {
41+
constructor(elementRef:ElementRef, loader:DynamicComponentLoader, router:routerMod.Router, injector:Injector, @Attribute('name') nameAttr:String) {
4142
if (isBlank(nameAttr)) {
4243
nameAttr = 'default';
4344
}
45+
this._loader = loader;
4446
this._parentRouter = router;
45-
this._childRouter = null;
46-
this._viewContainer = viewContainer;
47-
this._compiler = compiler;
47+
this._elementRef = elementRef;
4848
this._injector = injector;
49+
50+
this._childRouter = null;
51+
this._componentRef = null;
4952
this._parentRouter.registerOutlet(this, nameAttr);
5053
}
5154

5255
/**
5356
* Given an instruction, update the contents of this viewport.
5457
*/
5558
activate(instruction:Instruction): Promise {
56-
// if we're able to reuse the component, we just have to pass along the
59+
// if we're able to reuse the component, we just have to pass along the instruction to the component's router
60+
// so it can propagate changes to its children
5761
if (instruction.reuse && isPresent(this._childRouter)) {
5862
return this._childRouter.commit(instruction);
5963
}
60-
return this._compiler.compileInHost(instruction.component).then((pv) => {
61-
this._childRouter = this._parentRouter.childRouter(instruction.component);
62-
var outletInjector = this._injector.resolveAndCreateChild([
63-
bind(RouteParams).toValue(new RouteParams(instruction.params)),
64-
bind(routerMod.Router).toValue(this._childRouter)
65-
]);
6664

67-
this._viewContainer.clear();
68-
this._viewContainer.create(pv, 0, null, outletInjector);
65+
this._childRouter = this._parentRouter.childRouter(instruction.component);
66+
var outletInjector = this._injector.resolveAndCreateChild([
67+
bind(RouteParams).toValue(new RouteParams(instruction.params)),
68+
bind(routerMod.Router).toValue(this._childRouter)
69+
]);
70+
71+
if (isPresent(this._componentRef)) {
72+
this._componentRef.dispose();
73+
}
74+
return this._loader.loadNextToExistingLocation(instruction.component, this._elementRef, outletInjector).then((componentRef) => {
75+
this._componentRef = componentRef;
6976
return this._childRouter.commit(instruction);
7077
});
7178
}
7279

7380
deactivate():Promise {
7481
return (isPresent(this._childRouter) ? this._childRouter.deactivate() : PromiseWrapper.resolve(true))
75-
.then((_) => this._viewContainer.clear());
82+
.then((_) =>this._componentRef.dispose());
7683
}
7784

7885
canDeactivate(instruction:Instruction): Promise<boolean> {

0 commit comments

Comments
 (0)