Javascript/Jquery Callback for Fancybox Yes No Confirm

前端 未结 2 1921
南笙
南笙 2021-01-06 19:12

I\'m trying to implement a fancy Yes No confirm and I\'m having problems getting the callback to work as needed. The example below is using Fancybox v2.

The problem

相关标签:
2条回答
  • 2021-01-06 19:31

    Your method "do_something('a', 'b')" will be executed whenever user clicks on the link and the returned result will be passed as the second parameter to the "fancyConfirm" method. This is why your parameter "callback" is always "undefined".

    There is also a problem with fancyBox - "afterClose" callback gets called twice - before opening and after closing (this will be changed in the next release).

    I would bind a click event on the link and call a callback when user clicks on one of the buttons, like - http://jsfiddle.net/xcJFY/1/

    0 讨论(0)
  • 2021-01-06 19:38

    Not a big fan of the double callback method (Sorry Janis).

    So while trying to find a solution to this myself played around with similar code and found that as long my return value wasn't defined inside the scope of the function it works fine, when my return value was defined inside the function I would get weird behaviour where it would work for a time then revert to undefined.

    Effectively the return value needs to be outside the scope of the function, global, closure etc.

    $(document).ready(function() {
        $(".fancybox").on("click", function(e){   
            e.preventDefault();
            confirm("Do you wish to continue?", true, function(resp) {
                alert("You clicked " + resp);
            });
        });
    });
    
    function confirm(msg, modal, callback) {
        $.fancybox("#confirm",{
            modal: modal,
            beforeShow: function() {
                $(".title").html(msg);
            },
            afterShow: function() {
                $(".confirm").on("click", function(event){
                    if($(event.target).is(".yes")){
                        ret = true;
                    } else if ($(event.target).is(".no")){
                        ret = false;
                    }
                    $.fancybox.close();
                });
            },
            afterClose: function() {
                callback.call(this, ret);
            }
        });  
    }
    

    Here is a jsfiddle example

    Thanks to Francisco Diaz who's post on Google Groups FancyBox forum pointed me in the right direction.

    UPDATE:

    Have now extended my example to use dynamically built buttons and custom return values, so you are not just limited to true and false.

    $(document).ready(function() {
        $(".fancybox").on("click", function(e){   
            e.preventDefault();
            confirm("Do you wish to continue?", {
                    /* 
                    Define your buttons here, can handle multiple buttons 
                    with custom title and values
                    */
                    buttons: [
                        { class: "yes", type: "button", title: "Yes", value: "yes" },
                        { class: "no", type: "button", title: "No", value: "no" },
                        { class: "maybe", type: "button", title: "Maybe?", value: "maybe" }
                    ],
                    modal: true
                }, 
                function(resp) {
                    alert("You clicked " + resp);
                }
            );
        });
    });
    
    function confirm(msg, options, callback) {
        $.fancybox("#confirm",{
            modal: options.modal,
            beforeShow: function() {
                this.content.prepend("<p class=\"title\"></p>");
                $(".title").html(msg);
    
                for (i = 0; i < options.buttons.length; i++) {
                    this.content.append($("<input>", { 
                        type: "button", class: "confirm " + options.buttons[i].class, 
                        value: options.buttons[i].title
                      }).data("index", i));
                }
            },
            afterShow: function() {
                $(".confirm").on("click", function(event){
                    ret = options.buttons[$(event.target).data("index")].value;
                    $.fancybox.close();
                });
            },
            afterClose: function() {
                this.content.html("");
                callback.call(this, ret);
            }
        });
    }
    

    Added jsfiddle example

    0 讨论(0)
提交回复
热议问题