문제

I am facing some issues with the alert window being displayed. What I want is to display alert window and then based upon user's input I need to perform some action. But the problem I am facing is that I receive null as return value from the function.

PS: I am using Jquery msgbox v1.0 for alerts.

Here is the calling function code block -

var retVal = "No"; //don't save
    if($(this).parent().parent().hasClass("modified")){
        retVal = showPrompt();//Null returned
    }
    alert(retVal);
    switch (retVal) {
    case "Yes":
        //show download xml file on machine.            
        removeWorkspace(this);
        break;
    case "No":
        removeWorkspace(this);
        break;
    case "Cancel": 
        //don't do anything
        break;      
    }   
    event.stopPropagation();
}); 

Called Function:

function showPrompt(){
var resultVar = null;
$.msgBox({
    title: "Are you sure",
    content: "Do you want to save your work?",
    type: "confirm",
    buttons: [{ type:"submit", value: "Yes"},
            {type: "submit", value: "No"},
            {type: "cancel", value: "Cancel"}] 
}, function(result){
    resultVar = result;
});

    return resultVar;

}

Thanks in advance.

도움이 되었습니까?

해결책 2

msgBox does not behave as alert or confirm : when called, it does not suspend the current execution thread.

Your function returns before the user has clicked on any button.

The simplest way to fix this would be to call your removeWorkspace function from the "success" callback :

function showPrompt(ws){
    $.msgBox({
        title: "Are you sure",
        content: "Do you want to save your work?",
        type: "confirm",
        buttons: [{ type:"submit", value: "Yes"},
                  {type: "submit", value: "No"},
                  {type: "cancel", value: "Cancel"}] 
    , success: function(result){
        switch result {
            case "Yes" :
                removeWorkspace(ws);
                break;
            case "No" :
                removeWorkspace(ws);
                break;
        }
    }});
}

// change the calling site :
if($(this).parent().parent().hasClass("modified")){
    showPrompt(this);
} else {
    // default action :
    removeWorkspace(this);
}

다른 팁

In your showPrompt() function, resultVar is getting its value from a callback, but the function is being returned immediately, before that callback is executed, which is why resultVar is still null when showPrompt() returns.

Instead of trying to run the switch the way it is, why not move it to another function that is called from inside the callback?

var retVal = "No"; //don't save
var that = this;
if($(this).parent().parent().hasClass("modified")){
    showPrompt();
} else {
    methodWithSwitch(retVal);
}
event.stopPropagation();

function methodWithSwitch(val) {
    alert(val);
    switch (val) {
    case "Yes":
        //show download xml file on machine.            
        removeWorkspace(that);
        break;
    case "No":
        removeWorkspace(that);
        break;
    case "Cancel": 
        //don't do anything
        break;      
    }   
}

function showPrompt(){
    $.msgBox({
        title: "Are you sure",
        content: "Do you want to save your work?",
        type: "confirm",
        buttons: [{ type:"submit", value: "Yes"},
                {type: "submit", value: "No"},
                {type: "cancel", value: "Cancel"}] 
    }, function(result){
        methodWithSwitch(result);
    });
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top