Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise based dialogue vue js?

I have created a plugin but I don't know how to create a promise based. Can you tell me what I need to add to my existing code?

I use vuetify js for material style

NotifyDlg.vue: This contains the dialogue code for alert or confirm dialogue. Based on message type I will show/hide the button

<template>
  <v-dialog max-width="500px"
            v-model='dialogue'>
    <v-card>
      <v-card-title primary-title>
        <v-icon :color="messageType">{‌{messageType}}</v-icon>
        <title>{‌{title}}</title>
      </v-card-title>
      <v-card-text>{‌{message}}</v-card-text>
      <v-card-actions>
        <v-spacer></v-spacer>
        <v-btn :color="messageType"
               flat
               v-if="confirmDlg"
               @click="value=true">Yes</v-btn>
        <v-btn :color="confirmDlg?'':'primary'"
               flat
               @click="value=false">{‌{getBtnText()}}</v-btn>
      </v-card-actions>
    </v-card>
  </v-dialog>
</template>
<script>
export default {
  props: {
    confirmDlg: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '',
      required: true
    },
    message: {
      type: String,
      default: '',
      required: true
    },
    messageType: {
      type: String,
      default: 'warning',
      required: true
    }
  },
  data () {
    return {
      value: false,
      dialogue:false
    }
  },
  methods: {
    getBtnText () {
      if (this.confirmDlg) return 'No'
      return 'Ok'
    }
  }
}
</script>

NotifyDlgPlugin.js: Plugin install code. This method will then called in main.js using Vue.Use method.

import NotifyDlg from './NotifyDlg.vue'

export default {
  install: (Vue, options) => {
    Vue.prototype.$notifyDlg = {
      show (message, title, messageType, options = {}) {
        options.message = message
        options.title = title
        options.messageType = messageType
      }
    }
  }
}

from the documentation I got understanding of only global functions that can be called in install method. But I dont understand how can I call the dialogue I have created or how to return the true or false values to called method.

Any suggestion for my problem?

like image 632
Ajit Hegde Avatar asked Jan 27 '23 06:01

Ajit Hegde


1 Answers

I'd like to share my promise based dialog code:

import Dialog from "./Dialog.vue";

export function confirm(title, message) {
  return new Promise((resolve, reject) => {
    const dialog = new Vue({
      methods: {
        closeHandler(fn, arg) {
          return function() {
            fn(arg);
            dialog.$destroy();
            dialog.$el.remove();
          };
        }
      },
      render(h) {
        return h(Dialog, {
          props: {
            title,
            message,
          },
          on: {
            confirm: this.closeHandler(resolve),
            cancel: this.closeHandler(reject, new Error('canceled'))
          }
        });
      }
    }).$mount();
    document.body.appendChild(dialog.$el);
  });
}

This creates the the dialog, adds it to the DOM and resolves when the Dialog triggers the this.$emit('confirm') event.

like image 156
Bob Fanger Avatar answered Jan 31 '23 23:01

Bob Fanger