I have a little sandbox here with two components. A list of objects ("Groups" for the example) and a confirmation modal. The modal is triggered when a delete button is clicked. (This sandbox is extracted from another post in which I asked for different ways to send the result of the modal to the parent component, the GroupList
).
Here's the modal component:
<template>
<b-modal
id="modalConfirmation"
title="Confirmation"
ok-variant="danger"
@cancel="resolvePromise(false)"
@ok="resolvePromise(true)"
@close="resolvePromise(false)"
>Are you sure you want to delete this row ?</b-modal>
</template>
<script>
export default {
name: "ModalConfirmation",
data() {
return {
group: null,
resolvePromise: null,
rejectPromise: null
};
},
methods: {
show(group) {
return new Promise((resolve, reject) => {
this.group = group;
this.$bvModal.show("modalConfirmation");
this.resolvePromise = resolve;
this.rejectPromise = reject;
});
}
}
};
</script>
The best solution for me was this one. However, while I understand the principle of JavaScript promises, I couldn't figure out how it works in this case. It works perfectly, but I don't like to use code that I don't understand.
In the ModalConfirmation
, for the b-modal
tag, these are the event that set the result of the modal. But how does vuejs / bootstrap-vue make the matching between that and the promise ?
@ok="resolvePromise(true)"
@cancel="resolvePromise(false)"
@close="resolvePromise(false)"
Because the promise constructor is called when the modal is showed and that's all...
Moreover, if I comment this
resolvePromise: null,
rejectPromise: null
in the modal component, it still works. Can someone explain me the flow of promise resolution in this case?