When the confirm
function is triggered and the modal is displayed, the value of this.deleting
is set to true
. However, if you anticipate that changing this.deleting
should result in a different view within the Vue component, this will not occur because the confirm function blocks the process.
To address this issue, it is recommended to encapsulate the native dialog handling for confirm
, alert
, and prompt
in their own function. This approach not only enables asynchronous opening/triggering of the dialogs but also allows for potential replacement with custom dialog boxes in the future.
By utilizing await
/async
and Promises, the following method can be employed:
Your Dialog Module
const dlgs = {}
dlgs.confirm = function (message) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(confirm(message))
},0)
})
}
Your Vue Component
<a
@click="destroy"
:class="['button', { 'is-loading': deleting }]"
>
Delete
</a>
data: () => ({
deleting: false
}),
async destroy () {
this.deleting = true
if (! await dlgs.confirm('Sure?')) {
this.deleting = false
return
}
// perform the deletion
}
Implementing custom dialog boxes using HTML offers the advantage of being able to update information in the background while the dialog box remains open.