Skip to content

Commit cdbf305

Browse files
willshowellkara
authored andcommitted
feat(dialog): add beforeClose method (#6377)
1 parent dfd3d71 commit cdbf305

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

src/demo-app/dialog/dialog-demo.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ <h2>Other options</h2>
7575
</md-card-content>
7676
</md-card>
7777

78-
<p>Last close result: {{lastCloseResult}}</p>
78+
<p>Last afterClosed result: {{lastAfterClosedResult}}</p>
79+
<p>Last beforeClose result: {{lastBeforeCloseResult}}</p>
7980

8081
<ng-template>
8182
I'm a template dialog. I've been opened {{numTemplateOpens}} times!

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {MdDialog, MdDialogRef, MD_DIALOG_DATA} from '@angular/material';
1111
})
1212
export class DialogDemo {
1313
dialogRef: MdDialogRef<JazzDialog> | null;
14-
lastCloseResult: string;
14+
lastAfterClosedResult: string;
15+
lastBeforeCloseResult: string;
1516
actionsAlignment: string;
1617
config = {
1718
disableClose: false,
@@ -51,8 +52,11 @@ export class DialogDemo {
5152
openJazz() {
5253
this.dialogRef = this.dialog.open(JazzDialog, this.config);
5354

55+
this.dialogRef.beforeClose().subscribe((result: string) => {
56+
this.lastBeforeCloseResult = result;
57+
});
5458
this.dialogRef.afterClosed().subscribe((result: string) => {
55-
this.lastCloseResult = result;
59+
this.lastAfterClosedResult = result;
5660
this.dialogRef = null;
5761
});
5862
}

src/lib/dialog/dialog-ref.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {MdDialogContainer} from './dialog-container';
1515

1616

1717
// TODO(jelbourn): resizing
18-
// TODO(jelbourn): afterOpen and beforeClose
18+
// TODO(jelbourn): afterOpen
1919

2020
// Counter for unique dialog ids.
2121
let uniqueId = 0;
@@ -33,6 +33,9 @@ export class MdDialogRef<T> {
3333
/** Subject for notifying the user that the dialog has finished closing. */
3434
private _afterClosed: Subject<any> = new Subject();
3535

36+
/** Subject for notifying the user that the dialog has started closing. */
37+
private _beforeClose: Subject<any> = new Subject();
38+
3639
/** Result to be passed to afterClosed. */
3740
private _result: any;
3841

@@ -63,7 +66,11 @@ export class MdDialogRef<T> {
6366
RxChain.from(this._containerInstance._animationStateChanged)
6467
.call(filter, event => event.phaseName === 'start')
6568
.call(first)
66-
.subscribe(() => this._overlayRef.detachBackdrop());
69+
.subscribe(() => {
70+
this._beforeClose.next(dialogResult);
71+
this._beforeClose.complete();
72+
this._overlayRef.detachBackdrop();
73+
});
6774

6875
this._containerInstance._startExitAnimation();
6976
}
@@ -75,6 +82,13 @@ export class MdDialogRef<T> {
7582
return this._afterClosed.asObservable();
7683
}
7784

85+
/**
86+
* Gets an observable that is notified when the dialog has started closing.
87+
*/
88+
beforeClose(): Observable<any> {
89+
return this._beforeClose.asObservable();
90+
}
91+
7892
/**
7993
* Updates the dialog's position.
8094
* @param position New dialog position.

src/lib/dialog/dialog.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,25 @@ describe('MdDialog', () => {
140140
});
141141
}));
142142

143+
it('should close a dialog and get back a result before it is closed', async(() => {
144+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
145+
146+
// beforeClose should emit before dialog container is destroyed
147+
const beforeCloseHandler = jasmine.createSpy('beforeClose callback').and.callFake(() => {
148+
expect(overlayContainerElement.querySelector('md-dialog-container'))
149+
.not.toBeNull('dialog container exists when beforeClose is called');
150+
});
151+
152+
dialogRef.beforeClose().subscribe(beforeCloseHandler);
153+
dialogRef.close('Bulbasaurus');
154+
viewContainerFixture.detectChanges();
155+
156+
viewContainerFixture.whenStable().then(() => {
157+
expect(beforeCloseHandler).toHaveBeenCalledWith('Bulbasaurus');
158+
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeNull();
159+
});
160+
}));
161+
143162
it('should close a dialog via the escape key', async(() => {
144163
dialog.open(PizzaMsg, {
145164
viewContainerRef: testViewContainerRef

0 commit comments

Comments
 (0)