代码之家  ›  专栏  ›  技术社区  ›  Steerpike

Jquery确认框

  •  7
  • Steerpike  · 技术社区  · 17 年前

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

    (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
    
    2 回复  |  直到 17 年前
        1
  •  4
  •   Justin Love    17 年前

    您可以将jxContainer传递给confirm/cancel函数。

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

        2
  •  0
  •   GitaarLAB    11 年前

    试试这样的:

    (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)定义为对象的私有成员。但他们应该能够访问主对象的外部范围。

    推荐文章