Skip to content

Latest commit

 

History

History
50 lines (39 loc) · 1.63 KB

how-to-prevent-view-closing.md

File metadata and controls

50 lines (39 loc) · 1.63 KB

SCION Workbench

SCION Workbench Projects Overview Changelog Contributing Sponsoring

How to prevent a view from closing

A view can register a CanClose guard to intercept or prevent the closing.

The passed callback function is called when the view is about to close. Return true to close the view or false to prevent closing. Instead of a boolean, the method can return a Promise or an Observable to perform an asynchronous operation, such as displaying a message box.

The following snippet asks the user whether to save changes.

import {inject} from '@angular/core';
import {WorkbenchMessageBoxService, WorkbenchView} from '@scion/workbench';

inject(WorkbenchView).canClose(async () => {
  if (!inject(WorkbenchView).dirty()) {
    return true;
  }
  const action = await inject(WorkbenchMessageBoxService).open('Do you want to save changes?', {
    actions: {
      yes: 'Yes',
      no: 'No',
      cancel: 'Cancel'
    }
  });

  switch (action) {
    case 'yes':
      // Store changes ...
      return true;
    case 'no':
      return true;
    default:
      return false;
  }
});