" /> " /> "/>

I need to replace confirm box with the bootbox.confirm

49 Views Asked by At

Consider:

var isSaveRequired = false;
    
     function saveChanges() {
        if(isSaveRequired ) {
            if(confirm("<%=strings.getText("This is come from server side")%>")) {
                isSaveRequired=false;
                return true;
            }
            else {
                return false;
            }
        }
        return true;
    }

Here I need to replace confirm with bootbox.confirm (in our project, the bootbox.js library is there already).

function saveChanges() {
    if (isSaveRequired) {
        bootbox.confirm('<%= strings.getText("this msg come from server side") %>', function(result) {
            if (result) {
                isSaveRequired = false;
                return true;
                
            }
        });
        return false; 
    }
    return true; 
}

But here the problem is the pop up (unsaved data will be lost) showing correct as only, but if I click OK then it's not redirecting to another page based user choice.

But here the problem is the pop up (unsaved data will be lost) showing correct as only, but if I click OK then it needs to be navigating to another page based user choice.

1

There are 1 best solutions below

0
Tieson T. On

We note this in the docs in quite a few places: Bootstrap modals (and therefore Bootbox modals) are asynchronous:

All programmatic API methods are asynchronous and return to the caller once the transition is started, but before it ends.

So in this case:

function saveChanges() {
    if (isSaveRequired) {
        bootbox.confirm('<%= strings.getText("this msg come from server side") %>', function(result) {
            if (result) {
                isSaveRequired = false;
                return true;
                
            }
        });
        return false; 
    }
    return true; 
}

saveChanges() will complete while the dialog is still active. So, if you want something like a redirect to happen only if the user confirms something, you'd probably want something like:

function saveChanges() {
    if (isSaveRequired) {
        bootbox.confirm('<%= strings.getText("this msg come from server side") %>', function(result) {
            if (result) {
                // not sure where this comes from or how it's used, 
                // so leaving it here
                isSaveRequired = false;

                // perform a JS redirect - location.href is one option, or 
                // use something from: 
                // https://developer.mozilla.org/en-US/docs/Web/API/Window/location
                window.location.href = 'your redirect URL here';
            }
        });
        
        // assuming this is triggered by a button or anchor element,
        // 'return false' short-circuits the normal action
        return false; 
    }

    return true; 
}

Disclosure: I am one of the devs for the Bootbox project