Skip to content

Commit 111f0b0

Browse files
committed
ensure only one dialog content is attached
1 parent dee78cb commit 111f0b0

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/components/dialog/dialog-container.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import {PortalHostDirective} from '@angular2-material/core/portal/portal-directives';
88
import {PromiseCompleter} from '@angular2-material/core/async/promise-completer';
99
import {MdDialogConfig} from './dialog-config';
10+
import {MdDialogContentAlreadyAttachedError} from './dialog-errors';
1011

1112

1213
/**
@@ -49,20 +50,24 @@ export class MdDialogContainer extends BasePortalHost implements AfterViewInit {
4950

5051
this._deferredAttachPortal = null;
5152
this._deferredAttachCompleter = null;
52-
});
53+
}, () => this._deferredAttachCompleter.reject());
5354
}
5455
}
5556

5657
/** Attach a portal as content to this dialog container. */
5758
attachComponentPortal<T>(portal: ComponentPortal<T>): Promise<ComponentRef<T>> {
5859
if (this._portalHost) {
60+
if (this._portalHost.hasAttached()) {
61+
throw new MdDialogContentAlreadyAttachedError();
62+
}
63+
5964
return this._portalHost.attachComponentPortal(portal);
6065
} else {
6166
// The @ViewChild query for the portalHost is not resolved until AfterViewInit, but this
6267
// function may be called before this lifecycle event. As such, we defer the attachment of
6368
// the portal until AfterViewInit.
6469
if (this._deferredAttachCompleter) {
65-
this._deferredAttachCompleter.reject();
70+
throw new MdDialogContentAlreadyAttachedError();
6671
}
6772

6873
this._deferredAttachPortal = portal;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {MdError} from '@angular2-material/core/errors/error';
2+
3+
/** Exception thrown when a ComponentPortal is attached to a DomPortalHost without an origin. */
4+
export class MdDialogContentAlreadyAttachedError extends MdError {
5+
constructor() {
6+
super('Attempting to attach dialog content after content is already attached');
7+
}
8+
}

src/components/dialog/dialog.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('MdDialog', () => {
8989

9090
/** Runs the necessary detectChanges for a dialog to complete its opening. */
9191
function detectChangesForDialogOpen(fixture: ComponentFixture<ComponentWithChildViewContainer>) {
92-
// TODO(jelbourn): figure out why the test zone is "stable" when where are still pending
92+
// TODO(jelbourn): figure out why the test zone is "stable" when there are still pending
9393
// tasks, such that we have to use `setTimeout` to run the second round of change detection.
9494
// Two rounds of change detection are necessary: one to *create* the dialog container, and
9595
// another to cause the lifecycle events of the container to run and load the dialog content.

0 commit comments

Comments
 (0)