Permalink
Browse files

feat(dialog-settings, dialog-service): add childContainer

  • Loading branch information...
StrahilKazlachev committed Mar 17, 2017
1 parent 4aebd63 commit db0aca5cc3a994a67915e71ff1a8a7a0ba3fdf5c
Showing with 27 additions and 2 deletions.
  1. +3 −2 src/dialog-service.ts
  2. +7 −0 src/dialog-settings.ts
  3. +17 −0 test/unit/dialog-service.spec.ts
View
@@ -140,15 +140,16 @@ export class DialogService {
public open(settings?: DialogSettings & { rejectOnCancel?: false | boolean }): DialogOpenPromise<DialogCancellableOpenResult>;
public open(settings: DialogSettings = {}): DialogOpenPromise<DialogCancellableOpenResult> {
// tslint:enable:max-line-length
- const childContainer = this.container.createChild();
+ settings = this.createSettings(settings);
+ const childContainer = settings.childContainer || this.container.createChild();
let resolveCloseResult: any;
let rejectCloseResult: any;
const closeResult: Promise<DialogCloseResult> = new Promise((resolve, reject) => {
resolveCloseResult = resolve;
rejectCloseResult = reject;
});
const dialogController =
- childContainer.invoke(DialogController, [this.createSettings(settings), resolveCloseResult, rejectCloseResult]);
+ childContainer.invoke(DialogController, [settings, resolveCloseResult, rejectCloseResult]);
childContainer.registerInstance(DialogController, dialogController);
closeResult.then(() => {
removeController(this, dialogController);
View
@@ -1,3 +1,4 @@
+import { Container } from 'aurelia-dependency-injection';
import { ViewStrategy } from 'aurelia-templating';
export type ActionKey = 'Escape' | 'Enter';
@@ -28,6 +29,12 @@ export interface DialogSettings {
*/
host?: Element;
+ /**
+ * The child Container for the dialog creation.
+ * One will be created from the root if not provided.
+ */
+ childContainer?: Container;
+
/**
* When set to "false" allows the dialog to be closed with ESC key or clicking outside the dialog.
* When set to "true" the dialog does not close on ESC key or clicking outside of it.
@@ -113,6 +113,23 @@ describe('DialogService', () => {
done();
});
+ it('should create new child Container if "childContainer" is missing', async done => {
+ spyOn(container, 'createChild').and.callThrough();
+ await _success(() => dialogService.open(), done);
+ expect(container.createChild).toHaveBeenCalled();
+ done();
+ });
+
+ it('should not create new child Container if "childContainer" is provided', async done => {
+ const settings = { childContainer: container.createChild() };
+ spyOn(container, 'createChild').and.callThrough();
+ spyOn(settings.childContainer, 'invoke').and.callThrough();
+ await _success(() => dialogService.open(settings), done);
+ expect(container.createChild).not.toHaveBeenCalled();
+ expect(settings.childContainer.invoke).toHaveBeenCalled();
+ done();
+ });
+
it('propagates errors', async done => {
const expectdError = new Error('Expected error.');
spyOn(TestElement.prototype, 'canActivate').and.callFake(() => { throw expectdError; });

0 comments on commit db0aca5

Please sign in to comment.