Skip to content

vue-mvvm / dialog / DialogService

DialogService ​

Represents an MVVM service for instantiating dialogs from any ViewModel

Constructors ​

Constructor ​

ts
new DialogService(): DialogService;

Returns ​

DialogService

Methods ​

initDialog() ​

ts
initDialog<Instance, Arguments>(cls, ...args): Instance;

Renders a new instance of a DialogControl using the provided arguments.

Type Parameters ​

Instance ​

Instance extends DialogControl

Arguments ​

Arguments extends unknown[]

Parameters ​

cls ​

DialogControlConstructor<Instance, Arguments>

The class constructor for the DialogControl.

args ​

...Arguments

The arguments to pass to the dialog control constructor.

Returns ​

Instance

The newly created instance of the dialog control.

Example ​

ts
class MyDialogControl extends DialogControl {
    public isOpen: Ref<boolean> = ref(false);

    public constructor(message: string) {
        // Do something with the message...
    }

    public onOpen() {
        this.isOpen.value = true;
    }

    public onClose() {
        this.isOpen.value = false;

        // Since we only show a message, we can destroy the dialog afterward
        this.destroy();
    }
}

class MainViewModel extends ViewModel {
    private dialog: DialogService = this.ctx.getService(DialogService);

    // can be called multiple times
    public async openDialog(): Promise<void> {
        // instantiate a new dialog
        const dialog: MyDialogControl = this.dialog.initDialog(MyDialogControl, "Some message");

        // open the dialog
        await dialog.openDialog();

        // at this point we could call this.runAction(dialog) to await a dialog's action
    }
}