Skip to content

Commit 55bdfa8

Browse files
committed
feat(dialog): add beforeClose method
1 parent fd5e5ff commit 55bdfa8

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

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 {RxChain, first, filter} from '../core/rxjs/index';
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ describe('MdDialog', () => {
141141
});
142142
}));
143143

144+
it('should close a dialog and get back a result before it is closed', async(() => {
145+
let dialogRef = dialog.open(PizzaMsg, { viewContainerRef: testViewContainerRef });
146+
let beforeCloseCallback = jasmine.createSpy('beforeClose callback');
147+
148+
dialogRef.beforeClose().subscribe(beforeCloseCallback);
149+
dialogRef.close('Bulbasaurus');
150+
viewContainerFixture.detectChanges();
151+
152+
viewContainerFixture.whenStable().then(() => {
153+
expect(beforeCloseCallback).toHaveBeenCalledWith('Bulbasaurus');
154+
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeNull();
155+
});
156+
}));
157+
144158
it('should close a dialog via the escape key', async(() => {
145159
dialog.open(PizzaMsg, {
146160
viewContainerRef: testViewContainerRef

0 commit comments

Comments
 (0)