Skip to content

Instantly share code, notes, and snippets.

@tatat
Last active April 17, 2018 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatat/a8da6b18ae4dcd6297cd9726c860cab9 to your computer and use it in GitHub Desktop.
Save tatat/a8da6b18ae4dcd6297cd9726c860cab9 to your computer and use it in GitHub Desktop.
export default {
/**
* @param {Vue} Vue
* @param {any} options
*/
install(Vue, options) {
/**
* @typedef {object} MessageBox
* @property {boolean} confirmed - To be set true if confirmed, otherwise false
* @property {VueComponent} instance - Instance of VueComponent for $msgbox()
* @property {() => Promise.<string>} close - Close message box
*/
/**
* @name Vue#$messageBox
* @method
* @example
* const messageBox = await vm.$messageBox(options)
*
* try {
* if (messageBox.confirmed) {
* await onConfirmed()
* } else {
* await onCancelled()
* }
* } finally {
* await messageBox.close()
* }
* @param {any} options - To be passed to $msgbox() except "beforeClose"
* @return {Promise.<MessageBox>}
*/
Vue.prototype.$messageBox = function(options = {}) {
return new Promise((resolve, reject) => {
if (!this.$msgbox)
return reject(new Error('element-ui is required'))
let _action
const promiseClosed = this.$msgbox({
...options,
beforeClose(action, instance, done) {
_action = action
resolve({
confirmed: action === 'confirm',
instance,
close: () => {
done()
return promiseClosed
}
})
}
}).catch(error => {
if (error !== 'cancel')
throw error
}).then(() => _action)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment