Algorithm:
data[1-5]
someDataToShowInModal
.QUESTION: How to wait until user clicks modal's OK or CANCEL button?
In the code I show two possible solutions, but do not know how to implement them:
Wrap modal into Promise.
Use state.doSave
and somehow wait until it is changed by myModalComponent
.
Apply changes if user clicks OK.
Below is pseudo-code which shows the logic I try to implement:
state.js
modalTextSummary = {}
action.js
async myAction ({ state }) {
let modalClosed
let someDataToShowInModal = {}
let data1 = []
let data2 = []
let data3 = []
let data4 = []
let data5 = []
// #1. Push difference to "data[1-5]"
data1.push(xyz1)
data2.push(xyz2)
data3.push(xyz3)
data4.push(xyz4)
data5.push(xyz5)
// #2. Based on data[1-5] prepare "someDataToShowInModal"
someDataToShowInModal = {xyz}
// #3. We change "state.modalTextSummary" and by this we open
// a Modal (because "myModalCompont" has "watch: {modalTextSummary}")
state.modalTextSummary = someDataToShowInModal
// #4. HOW TO WAIT UNTIL USER CLICKS Modal's "OK" or "CANCEL"?
// v1:
// something like...
modalClosed = await myModalComponent
// v2:
// I can add "state.doSave = ''" which can be
// set by "myModalComponent" to either 'OK' or 'CANCEL', but how
// in this case I can wait for state changes?
modalClosed = await state.doSave !== ''
// #5. Apply changes
if (modalCloses === 'OK') {
// ... code which handles data from data[1-5]
}
}
myModalComponent.js
<script>
import { mapState } from 'vuex'
export default {
computed: {
...mapState([
'modalTextSummary'
])
},
watch: {
modalTextSummary: function () {
if (this.modalTextSummary !== {}) {
// Here I show bootstrap-vue modal
}
}
}
}
</script>
I know how to call a function once a modal is closed by OK button, but in this case it is necessary to temporary save data[1-5] in vuex in order to pick them again inside a called function and I want to avoid it using a simpler approach.
The X button on top and the close button on bottom can both hide/close the modal because of data-dismiss="modal" .
Inside the Open event handler of the jQuery UI Dialog Modal Popup box, using the JavaScript setTimeout function and the jQuery UI Dialog “close” command, the jQuery UI Dialog Modal Popup box is set to automatically close after delay of 5 seconds (some seconds). This dialog will automatically close in 5 seconds.
When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.
There are few ways to close modal in Bootstrap: click on a backdrop, close icon, or close button. You can also use JavaScript hide method. Click the button to launch the modal. Then click on the backdrop, close icon or close button to close the modal.
I want to offer you to make some refactor :)
1) Store`s actions SHOULD NOT know about interface (server-side-rendering issues, ...) - bad practice.
2) It is better to store modal's data in modal's parent component. Here is an example: https://jsfiddle.net/yyx990803/70yyx8z2/
Then you'll be able to do something like this (pseudocode):
<my-modal
v-if="showModal"
@cancel="processModalCancel"
@submit="processModalSubmit"
>
<!-- mutating-manipulations with modalData -->
</my-modal>
---
openModal() {
this.showModal = true;
this.modalData = store.dispatch('prepareDataToShowInModal'); // myAction, first part
},
processModalCancel() {
this.showModal = false;
this.modalData = null;
},
processModalSubmit() {
this.showModal = false;
store.dispatch('saveModalData', this.modalData); // myAction, second part
this.modalData = null;
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With