Determine if opened as dialog in prompt

173 Views Asked by At

I am developing an aurelia app. I have a component (which is a full-page component and is navigable) and also in another page, I want to use this component as a prompt to let user choose from that page. So I have written the code below to open it as intended:

selectTaskFromTree(callbackOrSuccess, failure) {
    const dialog = this.dialogService
      .open({ viewModel: PLATFORM.moduleName('features/view/tree/tree'), model: {}, lock: false });

    if (callbackOrSuccess) {
      if (failure) {
        dialog.whenClosed(response => {
          if (!response.wasCancelled) {
            callbackOrSuccess(response.output);
          } else {
            failure(response);
          }
        });
      }
      else{
        dialog.whenClosed(callbackOrSuccess);
      }
      return;
    }
    else{
      return dialog;
    }
  }

So the component Tree is now successfully loaded and shown. The problem is now how to determine if the TreeComponent is opened as a dialog or not.

The way I am thinking about is to pass an arbitrary param to it and if the param is true, the status is dialog and otherwise not dialog:

  const dialog = this.dialogService
      .open({ viewModel: PLATFORM.moduleName('features/view/tree/tree'), model: {isDialog: true}, lock: false });

But I think maybe there is also a better way to do this. For example to ask from DialogService if I am a dialog or not. So what are the other solutions and which one is better?

2

There are 2 best solutions below

0
ConductedClever On BEST ANSWER

I have thought about different solutions and I was seeking for keeping coupling low and preventing define of new param. So I wrote the below code where I want to know if this component is opened as a dialog or not:

  attached() {
    this._isDialog = Boolean(this.dialogController.settings);
  }
1
Henrik Erstad On

To determine whether or not you have an open (or active) dialog you can ask DialogService for the relevant fields:

this.dialogService.hasOpenDialog
this.dialogService.hasActiveDialog

Sadly, this cannot tell you which dialog is open. I would argue that your idea of passing a param to the model is an equally valid way of doing it.