jQuery Deferred and Dialog box

前端 未结 3 1875
function ValidateField(){
var bAllow= true;

    //some checking here

if (bAllow == true && apl.val().trim() == \"\")
{ 
    showDialog(); 
    showDialog()         


        
相关标签:
3条回答
  • 2020-11-27 05:35

    Well, This can work.

    Your dialog function... showDialog()

    function confirmation(question) {
        var defer = $.Deferred();
        $('<div></div>')
            .html(question)
            .dialog({
                autoOpen: true,
                modal: true,
                title: 'Confirmation',
                buttons: {
                    "Yes": function () {
                        defer.resolve("true");//this text 'true' can be anything. But for this usage, it should be true or false.
                        $(this).dialog("close");
                    },
                    "No": function () {
                        defer.resolve("false");//this text 'false' can be anything. But for this usage, it should be true or false.
                        $(this).dialog("close");
                    }
                },
                close: function () {
                    $(this).remove();
                }
            });
        return defer.promise();
    }
    

    and then the code where you use the function...

    function onclick(){
        var question = "Do you want to start a war?";
        confirmation(question).then(function (answer) {
            var ansbool = Boolean.parse(answer.toString());
            if(ansbool){
                alert("this is obviously " + ansbool);//TRUE
            } else {
                alert("and then there is " + ansbool);//FALSE
            }
        });
    }
    

    This may seem wrong for most people. But there is always some situations where you just can't go without return from JQuery Dialog.

    This will basically mimic the confirm() function. But with ugly code and a nice confirm box look :)

    I hope this helps some people out.


    EDIT: Bootstrap 3 Solution


    I am now using NakuPanda's bootstrap library, It works really well. Basically the same as with JQueryUI but in the Bootstrap UI.

    function bsConfirm(question) {
        var defer = $.Deferred();
        BootstrapDialog.show({
            type: BootstrapDialog.TYPE_PRIMARY,
            title: 'Confirmation',
            message: question,
            closeByBackdrop: false,
            closeByKeyboard: false,
            draggable: true,
            buttons: [{
                label: 'Yes',
                action: function (dialog) {
                    defer.resolve(true);
                    dialog.close();
                }
            }, {
                label: 'No',
                action: function (dialog) {
                    defer.resolve(false);
                    dialog.close();
                }
            }],
            close: function (dialog) {
                dialog.remove();
            }
        });
        return defer.promise();
    }
    function bsAlert(error, message) {
        BootstrapDialog.show({
            type: error ? BootstrapDialog.TYPE_DANGER : BootstrapDialog.TYPE_SUCCESS,
            title: error ? "Error" : "Success",
            message: message,
            closeByBackdrop: false,
            closeByKeyboard: false,
            draggable: true,
            buttons: [{
                label: 'OK',
                action: function (d) {
                    d.close();
                }
            }]
        });
    }
    

    and using it (Pretty much the same way)

    bsConfirm("Are you sure Bootstrap is what you wanted?").then(function (a) {
        if (a) {
            bsAlert("Well done! You have made the right choice");
        } else {
            bsAlert("I don't like you!");
        }
    });
    
    0 讨论(0)
  • 2020-11-27 05:51

    I have created this JSFIDDLE and changed the boolean parse because that was blowing up. Thanks, Pierre! This has saved me a lot of time.

    javascript:

    function confirmation(question) {
    var defer = $.Deferred();
    $('<div></div>')
        .html(question)
        .dialog({
            autoOpen: true,
            modal: true,
            title: 'Confirmation',
            buttons: {
                "Yes": function () {
                    defer.resolve("true");//this text 'true' can be anything. But for this usage, it should be true or false.
                    $(this).dialog("close");
                },
                "No": function () {
                    defer.resolve("false");//this text 'false' can be anything. But for this usage, it should be true or false.
                    $(this).dialog("close");
                }
            },
            close: function () {
                //$(this).remove();
                $(this).dialog('destroy').remove()
            }
        });
    return defer.promise();
    };
    
    function onclick(){
    var question = "Do you want to start a war?";
    confirmation(question).then(function (answer) {
        console.log(answer);
        var ansbool = (String(answer) == "true");
        if(ansbool){
            alert("this is obviously " + ansbool);//TRUE
        } else {
            alert("and then there is " + ansbool);//FALSE
        }
    });
    }
    
    $("#item").on('click', onclick);
    

    HTML:

    <button id="item">Hello, click me.</button>
    
    0 讨论(0)
  • 2020-11-27 05:55

    why not use the reject method instaed of resolve("false"). You will then be able to pass an object as argument. Let's say you have multiple fieldsets of inputs, each one having a delete button :

     function confirmation(question,obj) {
        var defer = $.Deferred();
        $('<div></div>')
            .html(question)
            .dialog({
                autoOpen: true,
                modal: true,
                title: 'Confirmation',
                buttons: {
                    "Oui": function () {
                        defer.resolve(obj);// pass the object to delete to the defer object
                        $(this).dialog("close");
                    },
                    "Non": function () {
                        defer.reject();//reject, no need to pass the object
                        $(this).dialog("close");
                    }
                },
                close: function () {
    
                    $(this).dialog('destroy').remove()
                }
            });
        return defer.promise();
    }
    
    $(document).on("click", ".btn-suppr",function (){// all delete buttons having a class btn-suppr
    
    var question = "Are you sure to delete this fieldset ?";
    confirmation(question,$(this)).then(function (obj) {
    
                                    obj.parent('fieldset').remove(); // remove the parent fieldset of the delete button if confirmed
    
                                });
                            });
    
    0 讨论(0)
提交回复
热议问题