Skip to content

Instantly share code, notes, and snippets.

@fimars
Created November 27, 2017 09:42
Show Gist options
  • Save fimars/64d68b1e9c1254b656bc7413caa20d56 to your computer and use it in GitHub Desktop.
Save fimars/64d68b1e9c1254b656bc7413caa20d56 to your computer and use it in GitHub Desktop.
A Vue Alert Demo
const alert = () => {
const VmConstructor = Vue.extend({
template: `
<div>
<button @click="$emit('yes')">yes</button>
<button @click="$emit('no')">no</button>
</div>
`
})
const instance = getAnInstance(VmConstructor)
mount(instance)
return new Promise((resolve, reject) => {
instance.$on('yes', (content = true) => {
resolve(content)
destroy(instance)
})
instance.$on('no', (message = 'cancel') => {
reject(createMessage(message))
destroy(instance)
})
})
}
function createMessage (content) {
return `[vv] ${conente}`
}
function mount(vm) {
document.body.appendChild(vm.$el)
}
function destroy(vm) {
document.body.removeChild(vm.$el)
vm.$destroy()
}
function getAnInstance(VmConstructor, propsData = {}) {
return new VmConstructor({
el: document.createElement('div'),
propsData
})
}
async function run () {
console.log(await alert())
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment