How to use SweetAlert2 in onPreClose event

How to use SweetAlert2 in onPreClose event

aftab ahmedaftab ahmed Posts: 5Questions: 2Answers: 0
edited September 2021 in Editor

This is my code below, as SweetAlert2 is async alert.

It is working to some extent, the editor close already before the preClose

editor.on('preClose', async function (e) {
                        // On close, check if the values have changed and ask for closing confirmation if they have
                        if (openVals !== JSON.stringify(Jobeditor_@(item.JobPrimaryID).get())) {
                            return   await editorConfirmAlert("You have unsaved changes. Are you sure you want to exit?", "Are you sure?")
                                    .then(function (result) {
                                        if (result.value) {
                                            return true
                                        } else {
                                            return false
                                        }
                                    });
                             //i am not using this confirm
                            //return confirm('You have unsaved changes. Are you sure you want to exit?');
                        }
                    })
                })

Answers

  • allanallan Posts: 61,323Questions: 1Answers: 10,023 Site admin

    The preClose event handler doesn't accept a Promise in the return I'm sorry to say. You'd need to return false from it and then call close() if the user wants to confirm the close (which will trigger preClose again, so you'd need a flag to indicate that it should complete!).

    Allan

Sign In or Register to comment.