Skip to content

Commit

Permalink
feat(dialog-controller): add param to canDeactivate / deactivate func…
Browse files Browse the repository at this point in the history
…tions.
  • Loading branch information
ydbondt committed Mar 31, 2017
1 parent 183c825 commit 42ad7f3
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/dialog-controller.ts
Expand Up @@ -45,8 +45,8 @@ export class DialogController {
/**
* @internal
*/
public releaseResources(): Promise<void> {
return invokeLifecycle(this.controller.viewModel || {}, 'deactivate')
public releaseResources(dialogResult?: any): Promise<void> {
return invokeLifecycle(this.controller.viewModel || {}, 'deactivate', dialogResult)
.then(() => this.renderer.hideDialog(this))
.then(() => { this.controller.unbind(); });
}
Expand Down Expand Up @@ -97,25 +97,28 @@ export class DialogController {
return this.closePromise;
}

return this.closePromise = invokeLifecycle(this.controller.viewModel || {}, 'canDeactivate').catch(reason => {
this.closePromise = undefined;
return Promise.reject(reason);
}).then(canDeactivate => {
if (!canDeactivate) {
this.closePromise = undefined; // we are done, do not block consecutive calls
return this.cancelOperation();
}
return this.releaseResources().then(() => {
if (!this.settings.rejectOnCancel || ok) {
this.resolve({ wasCancelled: !ok, output } as DialogCloseResult);
} else {
this.reject(createDialogCancelError(output));
}
return { wasCancelled: false };
}).catch(reason => {
const dialogResult = { wasCancelled: !ok, output } as DialogCloseResult;

return this.closePromise = invokeLifecycle(this.controller.viewModel || {}, 'canDeactivate', dialogResult)
.catch(reason => {
this.closePromise = undefined;
return Promise.reject(reason);
});
}).then(canDeactivate => {
if (!canDeactivate) {
this.closePromise = undefined; // we are done, do not block consecutive calls
return this.cancelOperation();
}
return this.releaseResources(dialogResult).then(() => {
if (!this.settings.rejectOnCancel || ok) {
this.resolve(dialogResult);
} else {
this.reject(createDialogCancelError(output));
}
return { wasCancelled: false };
}).catch(reason => {
this.closePromise = undefined;
return Promise.reject(reason);
});
});
}
}

0 comments on commit 42ad7f3

Please sign in to comment.