【问题标题】:Jquery confirmation boxjquery确认框
【发布时间】:2010-10-04 11:19:48
【问题描述】:

我正在寻找一个通用的确认框,可以轻松地被多个小部件使用,但我遇到了范围问题,并希望有一种更清晰的方式来做我想做的事情。

目前我有以下 -

(function() {

    var global = this;
    global.confirmationBox = function() {
    config = {
        container: '<div>',
        message:''
    }
    return {
        config: config,
        render: function(caller) {
            var jqContainer = $(config.container);
            jqContainer.append(config.message);
            jqContainer.dialog({
                buttons: {
                    'Confirm': caller.confirm_action,
                     Cancel: caller.cancel_action
                }
            });
        }
    }
} //end confirmationBox
global.testWidget = function() {
    return {
        create_message: function(msg) {
            var msg = confirmationBox();
            msg.message = msg;
            msg.render(this);
        },
        confirm_action: function() {
            //Do approved actions here and close the confirmation box
            //Currently not sure how to get the confirmation box at
            //this point
        },
        cancel_action: function() {
            //Close the confirmation box and register that action was 
            //cancelled with the widget. Like above, not sure how to get
            //the confirmation box  back to close it
        }
    }
}//end testWidget
})();
//Create the widget and pop up a test message
var widget = testWidget();
widget.create_message('You need to confirm this action to continue');

目前我只是想做一些简单的事情,比如从小部件中关闭盒子,但我认为我已经把自己的大脑包裹在知道什么的圈子里了。 有没有人想帮我清理一下我困惑的大脑?

干杯, 山姆

生成的代码:

我认为这可能对那些在以后发现此线程并寻找类似问题的解决方案的人有用,以查看我在此处获得的有用答案所产生的代码。

事实证明,最终它非常简单(就像大多数令人沮丧的思维纠葛一样)。

 /**
 * Confirmation boxes are used to confirm a request by a user such as
 * wanting to delete an item
 */
 global.confirmationBox = function() {
    self = this;
    config = {
        container: '<div>',
        message: '', 
    }
    return {
        set_config:config,
        render_message: function(caller) {
            var jqContainer = $(config.container);
            jqContainer.attr('id', 'confirmation-dialog');
            jqContainer.append(config.message);
            jqContainer.dialog({
               buttons: {
                   'Confirm': function() {
                       caller.confirm_action(this);
                    },
                   Cancel: function() {
                       caller.cancel_action(this);
                   }
               }
           });
        }
    }
 } // end confirmationBox

 global.testWidget = function() {
    return {
        create_message: function(msg) {
            var msg = confirmationBox();
            msg.message = msg;
            msg.render(this);
        },
        confirm_action: function(box) {
            alert('Success');
            $(box).dialog('close'); 
        },
        cancel_action: function(box) {
            alert('Cancelled');
            $(box).dialog('close'); 
        }
    }
}//end testWidget

【问题讨论】:

    标签: javascript jquery popup dialog


    【解决方案1】:

    试试这样的:

    (function() {
    
        var global = this;
        /*****************This is new****************/
        var jqContainer;
    
    
        global.confirmationBox = function() {
        config = {
            container: '<div>',
            message:''
        }
        return {
            config: config,
            render: function(caller) { 
    
                // store the container in the outer objects scope instead!!!!
                jqContainer = $(config.container);
    
                jqContainer.append(config.message);
                jqContainer.dialog({
                    buttons: {
                        'Confirm': caller.confirm_action,
                         Cancel: caller.cancel_action
                    }
                });
            }
        }
    } //end confirmationBox
    global.testWidget = function() {
        return {
            create_message: function(msg) {
                var msg = confirmationBox();
                msg.message = msg;
                msg.render(this);
            },
            confirm_action: function() {
                //Do approved actions here and close the confirmation box
                //Currently not sure how to get the confirmation box at this point
    
                /*******Hopefully, you would have access to jqContainer here now *****/
    
            },
            cancel_action: function() {
                //Close the confirmation box and register that action was 
                //cancelled with the widget. Like above, not sure how to get
                //the confirmation box  back to close it
            }
        }
    }//end testWidget
    })();
    //Create the widget and pop up a test message
    var widget = testWidget();
    widget.create_message('You need to confirm this action to continue');
    

    如果这不起作用,请尝试将回调(confirm_action、cancel_action)定义为对象的私有成员。但他们应该能够访问您的主要对象的外部范围。

    【讨论】:

      【解决方案2】:

      您可以将 jqContainer 传递给确认/取消函数。

      或者,将 jqContainer 分配为调用者的属性。由于确认/取消函数被调用为调用者的方法,它们将可以通过this 访问它。但这限制了您只能跟踪每个小部件一个对话框。

      【讨论】:

        猜你喜欢
        • 2011-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-12
        • 2011-08-11
        相关资源
        最近更新 更多