Skip to content

Commit 61b1367

Browse files
vakrilovvakrilov
authored andcommitted
Use directive instead of elementRef
1 parent 888f6d5 commit 61b1367

File tree

5 files changed

+77
-66
lines changed

5 files changed

+77
-66
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {Component} from 'angular2/core';
2+
import {ModalDialogParams} from "../../nativescript-angular/directives/dialogs";
3+
4+
@Component({
5+
selector: 'modal-content',
6+
template: `
7+
<StackLayout margin="24" horizontalAlignment="center" verticalAlignment="center">
8+
<Label [text]="prompt"></Label>
9+
<StackLayout orientation="horizontal" marginTop="12">
10+
<Button text="ok" (tap)="close('OK')"></Button>
11+
<Button text="cancel" (tap)="close('Cancel')"></Button>
12+
</StackLayout>
13+
</StackLayout>
14+
`
15+
})
16+
export class ModalContent {
17+
public prompt: string;
18+
constructor(private params: ModalDialogParams) {
19+
console.log("ModalContent.constructor: " + JSON.stringify(params))
20+
this.prompt = params.context.promptMsg;
21+
}
22+
23+
public close(res: string) {
24+
this.params.closeCallback(res);
25+
}
26+
27+
ngOnInit() {
28+
console.log("ModalContent.ngOnInit");
29+
30+
}
31+
32+
ngOnDestroy() {
33+
console.log("ModalContent.ngOnDestroy");
34+
}
35+
}

ng-sample/app/examples/modal/modal-test.ts

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,14 @@
1-
import {Component, DynamicComponentLoader, ElementRef, Injector, provide, Type, Injectable, Host, ComponentRef} from 'angular2/core';
2-
import {Page, ShownModallyData} from 'ui/page';
3-
import {View} from 'ui/core/view';
1+
import {Component} from 'angular2/core';
42
import * as dialogs from "ui/dialogs";
5-
import {ModalDialogService, ModalDialogOptions, ModalDialogParams} from "../../nativescript-angular/services/dialogs";
6-
7-
@Component({
8-
selector: 'modal-content',
9-
template: `
10-
<StackLayout margin="24" horizontalAlignment="center" verticalAlignment="center">
11-
<Label [text]="prompt"></Label>
12-
<StackLayout orientation="horizontal" marginTop="12">
13-
<Button text="ok" (tap)="close('OK')"></Button>
14-
<Button text="cancel" (tap)="close('Cancel')"></Button>
15-
</StackLayout>
16-
</StackLayout>
17-
`
18-
})
19-
export class ModalContent {
20-
public prompt: string;
21-
constructor(private params: ModalDialogParams) {
22-
console.log("ModalContent.constructor: " + JSON.stringify(params))
23-
this.prompt = params.context.promptMsg;
24-
}
25-
26-
public close(res: string) {
27-
this.params.closeCallback(res);
28-
}
29-
30-
ngOnInit() {
31-
console.log("ModalContent.ngOnInit");
32-
33-
}
34-
35-
ngOnDestroy() {
36-
console.log("ModalContent.ngOnDestroy");
37-
}
38-
}
3+
import {ModalDialogService, ModalDialogOptions, ModalDialogHost} from "../../nativescript-angular/directives/dialogs";
4+
import {ModalContent} from "./modal-content";
395

406
@Component({
417
selector: 'modal-test',
42-
directives: [],
8+
directives: [ModalDialogHost],
439
providers: [ModalDialogService],
4410
template: `
45-
<GridLayout rows="*, auto">
11+
<GridLayout rows="*, auto" modal-dialog-host>
4612
<StackLayout verticalAlignment="top" margin="12">
4713
<Button text="show component" (tap)="showModal(false)"></Button>
4814
<Button text="show component fullscreen" (tap)="showModal(true)"></Button>
@@ -62,19 +28,16 @@ export class ModalContent {
6228
export class ModalTest {
6329
public result: string = "result";
6430

65-
constructor(
66-
private elementRef: ElementRef,
67-
private modal: ModalDialogService) {
68-
}
69-
70-
public dialog() {
71-
dialogs.alert({ title: "alert title", message: "alert message", okButtonText: "KO, NE!" });
31+
constructor(private modal: ModalDialogService) {
7232
}
7333

7434
public showModal(fullscreen: boolean) {
75-
var options = new ModalDialogOptions({ promptMsg: "This is the prompt message!" }, fullscreen);
35+
var options: ModalDialogOptions = {
36+
context: { promptMsg: "This is the prompt message!" },
37+
fullscreen: fullscreen
38+
};
7639

77-
this.modal.showModal(ModalContent, this.elementRef, options).then((res: string) => {
40+
this.modal.showModal(ModalContent, options).then((res: string) => {
7841
this.result = res || "empty result";
7942
})
8043
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import {DynamicComponentLoader, ComponentRef, ElementRef, Component, Type} from 'angular2/core';
22

33
/**
4-
* Page shim used for loading compnenets when navigating
4+
* Wrapper component used for loading compnenets when navigating
5+
* It uses DetachedContainer as selector so that it is elementRef is not attached to the visual tree.
56
*/
67
@Component({
7-
selector: 'nativescript-page-shim',
8-
template: `
9-
<DetachedContainer>
10-
<Placeholder #loader></Placeholder>
11-
</DetachedContainer>
12-
`
8+
selector: 'DetachedContainer',
9+
template: `<Placeholder #loader></Placeholder>`
1310
})
1411
export class DetachedLoader {
1512
constructor(

src/nativescript-angular/services/dialogs.ts renamed to src/nativescript-angular/directives/dialogs.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import {DynamicComponentLoader, ElementRef, Injector, provide, Type, Injectable, ComponentRef} from 'angular2/core';
1+
import {DynamicComponentLoader, ElementRef, Injector, provide, Type, Injectable, ComponentRef, Directive} from 'angular2/core';
22
import {Page} from 'ui/page';
33
import {View} from 'ui/core/view';
44
import {DetachedLoader} from '../common/detached-loader';
55

6-
export class ModalDialogOptions {
7-
constructor(
8-
public context: any = {},
9-
public fullscreen: boolean = true) {
10-
}
6+
export interface ModalDialogOptions {
7+
context?: any;
8+
fullscreen?: boolean;
119
}
1210

1311
export class ModalDialogParams {
@@ -19,12 +17,21 @@ export class ModalDialogParams {
1917

2018
@Injectable()
2119
export class ModalDialogService {
20+
private elementRef: ElementRef;
21+
2222
constructor(
2323
private page: Page,
2424
private loader: DynamicComponentLoader) {
2525
}
2626

27-
public showModal(type: Type, elementRef: ElementRef, options: ModalDialogOptions): Promise<any> {
27+
public registerElementRef(ref: ElementRef) {
28+
this.elementRef = ref;
29+
}
30+
31+
public showModal(type: Type, options: ModalDialogOptions): Promise<any> {
32+
if (!this.elementRef) {
33+
throw new Error("No elementRef: Make sure you have the modal-dialog-host directive inside your component.");
34+
}
2835
return new Promise((resove, reject) => {
2936
const page = new Page();
3037

@@ -40,8 +47,8 @@ export class ModalDialogService {
4047
provide(Page, { useValue: page }),
4148
provide(ModalDialogParams, { useValue: modalParams }),
4249
]);
43-
44-
this.loader.loadNextToLocation(DetachedLoader, elementRef, providers)
50+
51+
this.loader.loadNextToLocation(DetachedLoader, this.elementRef, providers)
4552
.then((loaderRef) => {
4653
detachedLoaderRef = loaderRef;
4754
return (<DetachedLoader>loaderRef.instance).loadComponent(type)
@@ -57,4 +64,14 @@ export class ModalDialogService {
5764
});
5865
})
5966
}
60-
}
67+
}
68+
69+
70+
@Directive({
71+
selector: "[modal-dialog-host]"
72+
})
73+
export class ModalDialogHost {
74+
constructor(elementRef: ElementRef, modalService: ModalDialogService) {
75+
modalService.registerElementRef(elementRef);
76+
}
77+
}

src/nativescript-angular/view-util.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ export function createText(value: string): NgView {
117117
}
118118

119119
export function createViewContainer(name: string, parentElement: NgView, beforeAttach: BeforeAttachAction) {
120-
//HACK: Using a ContentView here, so that it creates a native View object
121120
traceLog('Creating view container in:' + parentElement);
122121

123122
const layout = createView('ProxyViewContainer', parentElement, beforeAttach);

0 commit comments

Comments
 (0)