Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e2e21a8

Browse files
committedMay 30, 2016
Merge pull request #247 from NativeScript/remove-dynamic-component-loader
Remove usages of Dynmamic ComponentLoader
2 parents 4c622d8 + 6c8682a commit e2e21a8

File tree

3 files changed

+38
-32
lines changed

3 files changed

+38
-32
lines changed
 

‎src/nativescript-angular/common/detached-loader.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {DynamicComponentLoader, ComponentRef, ViewContainerRef, Component, Type, ViewChild} from '@angular/core';
1+
import {ComponentRef, ViewContainerRef, Component, Type, ViewChild, ComponentResolver} from '@angular/core';
22

33
type AnyComponentRef = ComponentRef<any>;
44
interface PendingLoadEntry {
@@ -11,23 +11,20 @@ interface PendingLoadEntry {
1111
* It uses DetachedContainer as selector so that it is containerRef is not attached to the visual tree.
1212
*/
1313
@Component({
14-
selector: 'DetachedContainer',
14+
selector: 'DetachedContainer',
1515
template: `<Placeholder #loader></Placeholder>`
1616
})
1717
export class DetachedLoader {
1818
@ViewChild('loader', { read: ViewContainerRef }) containerRef: ViewContainerRef;
19-
private viewLoaded = false;
2019

20+
private viewLoaded = false;
2121
private pendingLoads: PendingLoadEntry[] = [];
2222

23-
constructor(
24-
private loader: DynamicComponentLoader
25-
) {
23+
constructor(private compiler: ComponentResolver) {
2624
}
2725

2826
public ngAfterViewInit() {
2927
this.viewLoaded = true;
30-
3128
this.pendingLoads.forEach(loadEntry => {
3229
this.loadInLocation(loadEntry.componentType).then(loadedRef => {
3330
loadEntry.resolveCallback(loadedRef);
@@ -36,7 +33,9 @@ export class DetachedLoader {
3633
}
3734

3835
private loadInLocation(componentType: Type): Promise<ComponentRef<any>> {
39-
return this.loader.loadNextToLocation(componentType, this.containerRef);
36+
return this.compiler.resolveComponent(componentType).then((componentFactory) => {
37+
return this.containerRef.createComponent(componentFactory, this.containerRef.length, this.containerRef.parentInjector, null);
38+
});
4039
}
4140

4241
public loadComponent(componentType: Type): Promise<ComponentRef<any>> {

‎src/nativescript-angular/directives/dialogs.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ReflectiveInjector, DynamicComponentLoader, ViewContainerRef, Injector, provide, Type, Injectable, ComponentRef, Directive} from '@angular/core';
1+
import {ReflectiveInjector, ComponentResolver, ViewContainerRef, Injector, provide, Type, Injectable, ComponentRef, Directive} from '@angular/core';
22
import {Page} from 'ui/page';
33
import {View} from 'ui/core/view';
44
import {DetachedLoader} from '../common/detached-loader';
@@ -21,7 +21,7 @@ export class ModalDialogService {
2121

2222
constructor(
2323
private page: Page,
24-
private loader: DynamicComponentLoader) {
24+
private compiler: ComponentResolver) {
2525
}
2626

2727
public registerViewContainerRef(ref: ViewContainerRef) {
@@ -48,21 +48,22 @@ export class ModalDialogService {
4848
provide(ModalDialogParams, { useValue: modalParams }),
4949
]);
5050

51-
this.loader.loadNextToLocation(DetachedLoader, this.containerRef, providers)
52-
.then((loaderRef) => {
53-
detachedLoaderRef = loaderRef;
54-
return (<DetachedLoader>loaderRef.instance).loadComponent(type)
55-
})
56-
.then((compRef) => {
57-
//Component loaded. Find its root native view.
51+
this.compiler.resolveComponent(DetachedLoader).then((detachedFactory) => {
52+
const childInjector = ReflectiveInjector.fromResolvedProviders(providers, this.containerRef.parentInjector);
53+
detachedLoaderRef = this.containerRef.createComponent(detachedFactory, -1, childInjector, null)
54+
55+
detachedLoaderRef.instance.loadComponent(type).then((compRef) => {
5856
const componentView = <View>compRef.location.nativeElement;
57+
5958
if (componentView.parent) {
6059
(<any>componentView.parent).removeChild(componentView);
6160
}
61+
6262
page.content = componentView;
6363
this.page.showModal(page, options.context, closeCallback, options.fullscreen);
6464
});
65-
})
65+
});
66+
});
6667
}
6768
}
6869

‎src/nativescript-angular/router/page-router-outlet.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import {isBlank, isPresent} from '@angular/core/src/facade/lang';
33
import {StringMapWrapper} from '@angular/core/src/facade/collection';
44

55
import {
6-
Attribute, DynamicComponentLoader, ComponentRef,
6+
Attribute, ComponentRef,
77
ViewContainerRef, ViewChild, ElementRef,
88
ReflectiveInjector, provide, Type,
9-
Component, Inject
9+
Component, Inject, DynamicComponentLoader, ComponentResolver
1010
} from '@angular/core';
1111

1212
import * as routerHooks from '@angular/router-deprecated/src/lifecycle/lifecycle_annotations';
1313
import {hasLifecycleHook} from '@angular/router-deprecated/src/lifecycle/route_lifecycle_reflector';
1414

15-
import {Router, RouterOutlet, RouteData, RouteParams, ComponentInstruction,
15+
import {Router, RouterOutlet, RouteData, RouteParams, ComponentInstruction,
1616
OnActivate, OnDeactivate, OnReuse, CanReuse} from '@angular/router-deprecated';
1717
import {LocationStrategy} from '@angular/common';
1818
import {topmost} from "ui/frame";
@@ -78,12 +78,13 @@ export class PageRouterOutlet extends RouterOutlet {
7878

7979
constructor(
8080
private containerRef: ViewContainerRef,
81-
private loader: DynamicComponentLoader,
81+
private compiler: ComponentResolver,
8282
private parentRouter: Router,
8383
@Attribute('name') nameAttr: string,
8484
private location: NSLocationStrategy,
85+
loader: DynamicComponentLoader,
8586
@Inject(DEVICE) device: Device
86-
) {
87+
) {
8788
super(containerRef, loader, parentRouter, nameAttr);
8889
this.viewUtil = new ViewUtil(device);
8990
}
@@ -135,20 +136,25 @@ export class PageRouterOutlet extends RouterOutlet {
135136
if (this.isInitalPage) {
136137
log("PageRouterOutlet.activate() inital page - just load component: " + componentType.name);
137138
this.isInitalPage = false;
138-
resultPromise = this.loader.loadNextToLocation(componentType, this.containerRef, ReflectiveInjector.resolve(providersArray));
139+
resultPromise = this.compiler.resolveComponent(componentType).then((componentFactory) => {
140+
const childInjector = ReflectiveInjector.resolveAndCreate(providersArray, this.containerRef.parentInjector);
141+
return this.containerRef.createComponent(componentFactory, this.containerRef.length, childInjector, null);
142+
});
139143
} else {
140144
log("PageRouterOutlet.activate() forward navigation - create detached loader in the loader container: " + componentType.name);
141145

142146
const page = new Page();
143147
providersArray.push(provide(Page, { useValue: page }));
144-
resultPromise = this.loader.loadNextToLocation(DetachedLoader, this.childContainerRef, ReflectiveInjector.resolve(providersArray))
145-
.then((pageComponentRef) => {
146-
loaderRef = pageComponentRef;
147-
return (<DetachedLoader>loaderRef.instance).loadComponent(componentType);
148-
})
149-
.then((actualCoponenetRef) => {
150-
return this.loadComponentInPage(page, actualCoponenetRef);
151-
})
148+
const childInjector = ReflectiveInjector.resolveAndCreate(providersArray, this.containerRef.parentInjector);
149+
150+
resultPromise = this.compiler.resolveComponent(DetachedLoader).then((componentFactory) => {
151+
loaderRef = this.childContainerRef.createComponent(componentFactory, this.childContainerRef.length, childInjector, null);
152+
153+
return (<DetachedLoader>loaderRef.instance).loadComponent(componentType)
154+
.then((actualCoponenetRef) => {
155+
return this.loadComponentInPage(page, actualCoponenetRef);
156+
});
157+
});
152158
}
153159

154160
return resultPromise.then((componentRef) => {

0 commit comments

Comments
 (0)
Please sign in to comment.