Skip to content

Commit ba4119b

Browse files
committed
Switch to using an OpaqueToken.
1 parent c819a32 commit ba4119b

File tree

7 files changed

+46
-38
lines changed

7 files changed

+46
-38
lines changed

src/demo-app/dialog/dialog-demo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {Component} from '@angular/core';
2-
import {MdDialog, MdDialogRef, MdDialogConfig, MdDialogData} from '@angular/material';
1+
import {Component, Inject} from '@angular/core';
2+
import {MdDialog, MdDialogRef, MdDialogConfig, MD_DIALOG_DATA} from '@angular/material';
33

44
@Component({
55
moduleId: module.id,
@@ -55,7 +55,7 @@ export class DialogDemo {
5555
export class JazzDialog {
5656
constructor(
5757
public dialogRef: MdDialogRef<JazzDialog>,
58-
public data: MdDialogData) { }
58+
@Inject(MD_DIALOG_DATA) public data: any) { }
5959
}
6060

6161

src/lib/dialog/README.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ MdDialog is a service, which opens dialogs components in the view.
1515
| --- | ------------ |
1616
| `role?: DialogRole = 'dialog'` | The ARIA role of the dialog element. Possible values are `dialog` and `alertdialog`. |
1717
| `disableClose?: boolean = false` | Whether to prevent the user from closing a dialog by clicking on the backdrop or pressing escape. |
18-
| `data?: { [key: string]: any }` | Data that will be injected into the dialog as `MdDialogData`. |
18+
| `data?: { [key: string]: any }` | Data that will be injected into the dialog as via the `MD_DIALOG_DATA` token. |
1919
| `width?: string = ''` | Width of the dialog. Takes any valid CSS value. |
2020
| `height?: string = ''` | Height of the dialog. Takes any valid CSS value. |
2121
| `position?: { top?: string, bottom?: string, left?: string, right?: string }` | Position of the dialog that overrides the default centering in it's axis. |
@@ -70,23 +70,6 @@ export class PizzaComponent {
7070
});
7171
}
7272
}
73-
74-
@Component({
75-
selector: 'pizza-dialog',
76-
template: `
77-
<h1 md-dialog-title>Would you like to order a {{ data.pizzaType }} pizza?</h1>
78-
79-
<md-dialog-actions>
80-
<button (click)="dialogRef.close('yes')">Yes</button>
81-
<button md-dialog-close>No</button>
82-
</md-dialog-actions>
83-
`
84-
})
85-
export class PizzaDialog {
86-
constructor(
87-
public dialogRef: MdDialogRef<PizzaDialog>,
88-
public data: MdDialogData) { }
89-
}
9073
```
9174

9275
The dialog component should be declared in the list of entry components of the module:
@@ -106,3 +89,25 @@ The dialog component should be declared in the list of entry components of the m
10689
export class AppModule { }
10790

10891
```
92+
93+
## Injecting data
94+
If you want to pass in extra data to your dialog instance, you can do so
95+
by using the `MD_DIALOG_DATA` injection token.
96+
```
97+
@Component({
98+
selector: 'pizza-dialog',
99+
template: `
100+
<h1 md-dialog-title>Would you like to order a {{ data.pizzaType }} pizza?</h1>
101+
102+
<md-dialog-actions>
103+
<button (click)="dialogRef.close('yes')">Yes</button>
104+
<button md-dialog-close>No</button>
105+
</md-dialog-actions>
106+
`
107+
})
108+
export class PizzaDialog {
109+
constructor(
110+
public dialogRef: MdDialogRef<PizzaDialog>,
111+
@Inject(MD_DIALOG_DATA) public data: any) { }
112+
}
113+
```

src/lib/dialog/dialog-config.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ export interface DialogPosition {
1111
right?: string;
1212
};
1313

14-
/** Data to be injected into a dialog. */
15-
export class MdDialogData {
16-
[key: string]: any;
17-
}
18-
1914
/**
2015
* Configuration for opening a modal dialog with the MdDialog service.
2116
*/
@@ -38,7 +33,7 @@ export class MdDialogConfig {
3833
position?: DialogPosition;
3934

4035
/** Data being injected into the child component. */
41-
data?: MdDialogData;
36+
data?: any;
4237

4338
// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
4439
}

src/lib/dialog/dialog-injector.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import {Injector} from '@angular/core';
1+
import {Injector, OpaqueToken} from '@angular/core';
22
import {MdDialogRef} from './dialog-ref';
3-
import {MdDialogData} from './dialog-config';
43

4+
export const MD_DIALOG_DATA = new OpaqueToken('MdDialogData');
55

66
/** Custom injector type specifically for instantiating components with a dialog. */
77
export class DialogInjector implements Injector {
88
constructor(
9+
private _parentInjector: Injector,
910
private _dialogRef: MdDialogRef<any>,
10-
private _data: MdDialogData,
11-
private _parentInjector: Injector) { }
11+
private _data: any) { }
1212

1313
get(token: any, notFoundValue?: any): any {
1414
if (token === MdDialogRef) {
1515
return this._dialogRef;
1616
}
1717

18-
if (token === MdDialogData && this._data) {
18+
if (token === MD_DIALOG_DATA && this._data) {
1919
return this._data;
2020
}
2121

src/lib/dialog/dialog.spec.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ import {
77
TestBed,
88
tick,
99
} from '@angular/core/testing';
10+
import {NgModule,
11+
Component,
12+
Directive,
13+
ViewChild,
14+
ViewContainerRef,
15+
Injector,
16+
Inject,
17+
} from '@angular/core';
1018
import {By} from '@angular/platform-browser';
11-
import {NgModule, Component, Directive, ViewChild, ViewContainerRef, Injector} from '@angular/core';
1219
import {MdDialogModule} from './index';
1320
import {MdDialog} from './dialog';
1421
import {OverlayContainer} from '../core';
1522
import {MdDialogRef} from './dialog-ref';
1623
import {MdDialogContainer} from './dialog-container';
17-
import {MdDialogData} from './dialog-config';
24+
import {MD_DIALOG_DATA} from './dialog-injector';
1825

1926

2027
describe('MdDialog', () => {
@@ -254,8 +261,8 @@ describe('MdDialog', () => {
254261

255262
let instance = dialog.open(DialogWithInjectedData, config).componentInstance;
256263

257-
expect(instance.data['stringParam']).toBe(config.data.stringParam);
258-
expect(instance.data['dateParam']).toBe(config.data.dateParam);
264+
expect(instance.data.stringParam).toBe(config.data.stringParam);
265+
expect(instance.data.dateParam).toBe(config.data.dateParam);
259266
});
260267

261268
it('should throw if injected data is expected but none is passed', () => {
@@ -502,7 +509,7 @@ class ComponentThatProvidesMdDialog {
502509
/** Simple component for testing ComponentPortal. */
503510
@Component({template: ''})
504511
class DialogWithInjectedData {
505-
constructor(public data: MdDialogData) { }
512+
constructor(@Inject(MD_DIALOG_DATA) public data: any) { }
506513
}
507514

508515
// Create a real (non-test) NgModule as a workaround for

src/lib/dialog/dialog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class MdDialog {
120120
// inject the MdDialogRef. This allows a component loaded inside of a dialog to close itself
121121
// and, optionally, to return a value.
122122
let userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
123-
let dialogInjector = new DialogInjector(dialogRef, config.data, userInjector || this._injector);
123+
let dialogInjector = new DialogInjector(userInjector || this._injector, dialogRef, config.data);
124124

125125
let contentPortal = new ComponentPortal(component, null, dialogInjector);
126126

src/lib/dialog/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ export * from './dialog-container';
5757
export * from './dialog-content-directives';
5858
export * from './dialog-config';
5959
export * from './dialog-ref';
60+
export {MD_DIALOG_DATA} from './dialog-injector';

0 commit comments

Comments
 (0)