I’m implementing a global confirm dialog feature.
For example: The confirm dialog will open when a user clicks a “publish” button to publish an article.
- The user clicks the publish button triggers the function “openConfirmDialog()”.
- Show the confirm dialog.
- Wait for the user to click the “confirm” button.
- The function( onConfirm() ) in “confirmDialog.vue” will be triggered when the confirm button is clicked.
Questions:
How can I pass and trigger a dynamic action(in this example: pubishArticle ) when the user click the “confirm” button?
Component – confirmDialog.vue Cancel button:
JavaScript
x
4
1
protected onCancel() {
2
this.$store.actions.closeConfirmDialog()
3
}
4
Confirm button:
JavaScript
1
4
1
protected onConfirm() {
2
this.$store.actions.proceedConfirmDialog()
3
}
4
confirm dialog vuex module store actions:
JavaScript
1
7
1
async openConfirmDialog(
2
context: ActionContext<ConfirmDialogState, any>,
3
payload: SetConfirmDialogPayloadParams
4
) {
5
context.commit(types.CONFIRM_DIALOG_SET_CONTENT, payload.content)
6
},
7
Confirm button action:
JavaScript
1
2
1
async proceedConfirmDialog(context: ActionContext<ConfirmDialogState, any>) {}
2
frontend views – demo.vue:
JavaScript
1
8
1
public openConfirmDialog() {
2
this.$store.myActions.openConfirmDialog({
3
content: {
4
message: 'are you sure?',
5
}
6
})
7
}
8
publish article vuex module store action:
JavaScript
1
2
1
async pubishArticle(){ .}
2
Advertisement
Answer
You could try sending the buttons definition inside openConfirmDialog
:
JavaScript
1
11
11
1
public openConfirmDialog() {
2
this.$store.myActions.openConfirmDialog({
3
content: { message: 'are you sure?' },
4
buttons: [
5
{ text: 'Confirm', onClick: this.onConfirmButtonClicked },
6
{ text: 'Cancel', onClick: this.onCancelButtonClicked },
7
],
8
9
})
10
}
11
OR
You could just emit a custom event on button click and let the parent component handle it.