允许调用函数覆盖默认选项 - jQuery UI 对话框

发布于 2024-10-12 20:19:27 字数 1215 浏览 5 评论 0原文

我希望 CallingFunction 能够覆盖 showDivPopUp 函数中提供的默认选项。

function calling(){
  showDivPopUp("title of pop up box", "message to show", 
        {
            buttons:{
                        Yes: function () {
                                $(this).dialog("destroy");
                            },
                        No :function () {
                                $(this).dialog("destroy");
                            }                        
                    }      
        });
}

function showDivPopUp(title,msg,options){
  var mgDiv = $("#msgDiv");
  mgDiv.attr("innerHTML", msg);
  return mgDiv.dialog({
    modal: true,
    buttons: {
      Ok: function () {
        $(this).dialog("destroy");
      }
    },
    resizable: true,
    show: "explode",
    position: "center",
    closeOnEscape: true,
    draggable: false,
    title : titl,
    open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); }
  });
}

因此,上面的代码应该显示两个按钮,即。 ,而不仅仅是确定。我不想对每个选项都进行 if 检查。

更新:
在选项参数中,可能存在不应用默认值的选项。因此调用函数可能会指定 showDivPopUp 函数中未提及的 size 选项。

I want the callingFunction to be able to override the default options provided in the showDivPopUp function.

function calling(){
  showDivPopUp("title of pop up box", "message to show", 
        {
            buttons:{
                        Yes: function () {
                                $(this).dialog("destroy");
                            },
                        No :function () {
                                $(this).dialog("destroy");
                            }                        
                    }      
        });
}

function showDivPopUp(title,msg,options){
  var mgDiv = $("#msgDiv");
  mgDiv.attr("innerHTML", msg);
  return mgDiv.dialog({
    modal: true,
    buttons: {
      Ok: function () {
        $(this).dialog("destroy");
      }
    },
    resizable: true,
    show: "explode",
    position: "center",
    closeOnEscape: true,
    draggable: false,
    title : titl,
    open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); }
  });
}

So, the above code should show two buttons viz. Yes and No instead of just OK. I don't want to do if check for each option.

UPDATE:
In options parameter there might be options for which default is not applied. So the calling function may specify size option which is not mentioned in the showDivPopUp function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一影成城 2024-10-19 20:19:27

您想要使用 JQuery Extend() 方法将传递到函数中的选项与函数中指定的默认值合并。

看:
http://www.zachstronaut.com/posts/2009/05/ 14/javascript-default-options-pattern.html

http://api.jquery.com/jQuery.extend/

//calling function source excluded, use exactly the same.

function showDivPopUp(title, msg, options) {

    //create basic default options
    var defaults = {
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog("destroy");
            }
        },
        resizable: true,
        show: "explode",
        position: "center",
        closeOnEscape: true,
        draggable: false,
        title: title,
        open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
    }

    //merge the specified options with the defaults.
    //in example case, will have the above except with the new buttons specified
    if (typeof options == 'object') {
        options = $.extend(defaults, options);
    } else {
        options = defaults;
    }


    var mgDiv = $("#msgDiv");
    mgDiv.attr("innerHTML", msg);
    return mgDiv.dialog(options); 
}

You want to use the JQuery extend() method to merge the options you pass into the function with the defaults that are specified within it.

See:
http://www.zachstronaut.com/posts/2009/05/14/javascript-default-options-pattern.html
and
http://api.jquery.com/jQuery.extend/

//calling function source excluded, use exactly the same.

function showDivPopUp(title, msg, options) {

    //create basic default options
    var defaults = {
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog("destroy");
            }
        },
        resizable: true,
        show: "explode",
        position: "center",
        closeOnEscape: true,
        draggable: false,
        title: title,
        open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
    }

    //merge the specified options with the defaults.
    //in example case, will have the above except with the new buttons specified
    if (typeof options == 'object') {
        options = $.extend(defaults, options);
    } else {
        options = defaults;
    }


    var mgDiv = $("#msgDiv");
    mgDiv.attr("innerHTML", msg);
    return mgDiv.dialog(options); 
}
绝影如岚 2024-10-19 20:19:27

看起来“选项”是 JSON 格式。尝试省略 showDivPopUp 的第三个参数中的第一个 {buttons: 部分,或在 showDivPopUp 函数中设置 buttons: options.buttons

要对此进行扩展,请创建更多 json 对,并测试它们在 showDivPopUp 函数中是否存在。存在吗?覆盖。不存在吗?保留默认值。

{buttons:{
  Yes: function () {
    $(this).dialog("destroy");
  },
  No :function () {
    $(this).dialog("destroy");
  }                        
},
background:"blue",
fontsize:15
}

通过以下方式访问每个:

options.buttons
options.background
options.fontsize

使用以下方式测试是否存在:

if ( typeof( window[ 'option.fontsize' ] ) != "undefined" ) {override code}

对问题中的更新的响应:
使用 jquery.each 迭代传递选项中的所有元素。

Looks like 'options' is in JSON format. Try omitting the first {buttons: portion in the 3rd argument to showDivPopUp or set buttons: options.buttons in the showDivPopUp function.

To expand on this, create more json pairs, and test for their existence in the showDivPopUp function. Exists? Override. Doesn't exist? Keep defaults.

{buttons:{
  Yes: function () {
    $(this).dialog("destroy");
  },
  No :function () {
    $(this).dialog("destroy");
  }                        
},
background:"blue",
fontsize:15
}

Access each via:

options.buttons
options.background
options.fontsize

Test for existence using:

if ( typeof( window[ 'option.fontsize' ] ) != "undefined" ) {override code}

Response to the update in the question:
Use jquery.each to iterate over all elements in the passed option.

浅唱ヾ落雨殇 2024-10-19 20:19:27

mgDiv.dialog 函数中,修改 buttons 键以具有条件值。例如:

function showDivPopUp(title,msg,options){
  var mgDiv = $("#msgDiv");
  mgDiv.attr("innerHTML", msg);
  return mgDiv.dialog({
    modal: true,
    buttons: options.buttons || {
      Ok: function () {
        $(this).dialog("destroy");
      }
    },
    resizable: true,
    show: "explode",
    position: "center",
    closeOnEscape: true,
    draggable: false,
    title : titl,
    open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); }
  });
}

In your mgDiv.dialog function, modify the buttons key to have a conditional value. E.g.:

function showDivPopUp(title,msg,options){
  var mgDiv = $("#msgDiv");
  mgDiv.attr("innerHTML", msg);
  return mgDiv.dialog({
    modal: true,
    buttons: options.buttons || {
      Ok: function () {
        $(this).dialog("destroy");
      }
    },
    resizable: true,
    show: "explode",
    position: "center",
    closeOnEscape: true,
    draggable: false,
    title : titl,
    open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); }
  });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文