Skip to content

Commit 681d063

Browse files
committed
feat(view): implemented loading component next to existing location
1 parent 77b31ab commit 681d063

File tree

8 files changed

+222
-204
lines changed

8 files changed

+222
-204
lines changed

modules/angular2/src/core/application.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function _injectorBindings(appComponentType): List<Binding> {
7575
// We need to do this here to ensure that we create Testability and
7676
// it's ready on the window for users.
7777
registry.registerApplication(appElement, testability);
78-
return dynamicComponentLoader.loadIntoNewLocation(appElement, appComponentAnnotatedType.type, null, injector);
78+
return dynamicComponentLoader.loadIntoNewLocation(appElement, appComponentAnnotatedType.type, injector);
7979
}, [DynamicComponentLoader, Injector, appElementToken, appComponentAnnotatedTypeToken,
8080
Testability, TestabilityRegistry]),
8181

modules/angular2/src/core/compiler/dynamic_component_loader.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ export class ComponentRef {
1616
location:ElementRef;
1717
instance:any;
1818
componentView:AppView;
19+
_dispose:Function;
1920

20-
constructor(location:ElementRef, instance:any, componentView:AppView){
21+
constructor(location:ElementRef, instance:any, componentView:AppView, dispose:Function){
2122
this.location = location;
2223
this.instance = instance;
2324
this.componentView = componentView;
25+
this._dispose = dispose;
2426
}
2527

2628
get injector() {
@@ -30,6 +32,10 @@ export class ComponentRef {
3032
get hostView() {
3133
return this.location.hostView;
3234
}
35+
36+
dispose() {
37+
this._dispose();
38+
}
3339
}
3440

3541
/**
@@ -72,16 +78,16 @@ export class DynamicComponentLoader {
7278
// from the component child views
7379
// See ViewFactory.returnView
7480
// See AppViewHydrator.dehydrateDynamicComponentView
75-
return new ComponentRef(location, location.elementInjector.getDynamicallyLoadedComponent(), componentView);
81+
var dispose = () => {throw "Not implemented";};
82+
return new ComponentRef(location, location.elementInjector.getDynamicallyLoadedComponent(), componentView, dispose);
7683
});
7784
}
7885

7986
/**
80-
* Loads a component as a child of the View given by the provided ElementRef. The loaded
81-
* component receives injection normally as a hosted view.
87+
* Loads a component in the element specified by elementOrSelector. The loaded component receives
88+
* injection normally as a hosted view.
8289
*/
83-
loadIntoNewLocation(elementOrSelector:any, type:Type, location:ElementRef,
84-
injector:Injector = null):Promise<ComponentRef> {
90+
loadIntoNewLocation(elementOrSelector:any, type:Type, injector:Injector = null):Promise<ComponentRef> {
8591
this._assertTypeIsComponent(type);
8692

8793
return this._compiler.compileInHost(type).then(hostProtoView => {
@@ -91,9 +97,30 @@ export class DynamicComponentLoader {
9197
// TODO(vsavkin): return a component ref that dehydrates the host view
9298
// See ViewFactory.returnView
9399
// See AppViewHydrator.dehydrateInPlaceHostView
94-
var newLocation = new ElementRef(hostView.elementInjectors[0]);
100+
var newLocation = hostView.elementInjectors[0].getElementRef();
101+
var component = hostView.elementInjectors[0].getComponent();
102+
var dispose = () => {throw "Not implemented";};
103+
return new ComponentRef(newLocation, component, hostView.componentChildViews[0], dispose);
104+
});
105+
}
106+
107+
/**
108+
* Loads a component next to the provided ElementRef. The loaded component receives
109+
* injection normally as a hosted view.
110+
*/
111+
loadNextToExistingLocation(type:Type, location:ElementRef, injector:Injector = null):Promise<ComponentRef> {
112+
this._assertTypeIsComponent(type);
113+
114+
return this._compiler.compileInHost(type).then(hostProtoView => {
115+
var hostView = location.viewContainer.create(-1, hostProtoView, injector);
116+
117+
var newLocation = hostView.elementInjectors[0].getElementRef();
95118
var component = hostView.elementInjectors[0].getComponent();
96-
return new ComponentRef(newLocation, component, hostView.componentChildViews[0]);
119+
var dispose = () => {
120+
var index = location.viewContainer.indexOf(hostView);
121+
location.viewContainer.remove(index);
122+
};
123+
return new ComponentRef(newLocation, component, hostView.componentChildViews[0], dispose);
97124
});
98125
}
99126

modules/angular2/src/core/compiler/element_injector.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,10 @@ export class ElementInjector extends TreeNode {
672672
}
673673
}
674674

675+
getElementRef() {
676+
return new ElementRef(this);
677+
}
678+
675679
getDynamicallyLoadedComponent() {
676680
return this._dynamicallyCreatedComponent;
677681
}
@@ -741,7 +745,7 @@ export class ElementInjector extends TreeNode {
741745
if (isPresent(dep.attributeName)) return this._buildAttribute(dep);
742746
if (isPresent(dep.queryDirective)) return this._findQuery(dep.queryDirective).list;
743747
if (dep.key.id === StaticKeys.instance().elementRefId) {
744-
return new ElementRef(this);
748+
return this.getElementRef();
745749
}
746750
return this._getByKey(dep.key, dep.depth, dep.optional, requestor);
747751
}

modules/angular2/src/core/compiler/view_container.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ export class ViewContainer {
9292
return view;
9393
}
9494

95+
indexOf(view:viewModule.AppView) {
96+
return ListWrapper.indexOf(this._views, view);
97+
}
98+
9599
remove(atIndex=-1) {
96100
if (atIndex == -1) atIndex = this._views.length - 1;
97101
var view = this._views[atIndex];

modules/angular2/src/facade/collection.es6

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ export class ListWrapper {
129129
static filter(array, pred:Function) {
130130
return array.filter(pred);
131131
}
132+
static indexOf(array, value, startIndex = -1) {
133+
return array.indexOf(value, startIndex);
134+
}
132135
static any(list:List, pred:Function) {
133136
for (var i = 0 ; i < list.length; ++i) {
134137
if (pred(list[i])) return true;

modules/angular2/src/facade/collection.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ export class ListWrapper {
114114
}
115115
return null;
116116
}
117+
static indexOf(array: List<any>, value, startIndex = -1) {
118+
return array.indexOf(value, startIndex);
119+
}
117120
static reduce<T>(list: List<T>,
118121
fn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T,
119122
init: T) {

0 commit comments

Comments
 (0)