JQuery UI对话框:应用程序范围的选项
我的应用程序中有大量jquery ui对话框。每次我需要一个新的,我写下以下几行:JQuery UI对话框:应用程序范围的选项,jquery,jquery-ui,jquery-dialog,Jquery,Jquery Ui,Jquery Dialog,我的应用程序中有大量jquery ui对话框。每次我需要一个新的,我写下以下几行: $('.another-dialog').dialog({ title: 'Another dialog', autoOpen: false, draggable: true, modal: true, show: 'fade', hide: 'fade', width: 400, position: ['center', 'center'], buttons: [ {
$('.another-dialog').dialog({
title: 'Another dialog',
autoOpen: false,
draggable: true,
modal: true,
show: 'fade',
hide: 'fade',
width: 400,
position: ['center', 'center'],
buttons: [
{ text: 'Ok' },
{ text: 'Cancel' }
],
open: function(event, ui) { $(".ui-dialog-titlebar-close span").html('×') }
});
一个对话框与另一个对话框之间真正不同的是按钮
和标题
键。我希望在这里为JQuery对话框设置一个应用程序范围的设置,所以我只调用
$('.another-dialog').dialog({
title: 'Another dialog',
buttons: [
{ text: 'Ok' },
{ text: 'Cancel' }
]
});
隐式设置了所有需要的散列键(我称之为“默认”设置)
我知道我可以把.dialog()
调用包装起来,比如说,.myDialog()
调用我自己设置的所有东西。但我想知道是否有一种真正方便的方法
提前谢谢 您可以将公共选项放在变量中(如果您想从不同的范围使用它们,也可以放在与文档关联的中): 然后,可以使用为每个对话框添加特定选项:
$(".another-dialog").dialog(
$.extend({}, $(document).data("common-dialog-options"), {
title: "Another dialog",
buttons: [
{ text: "OK" },
{ text: "Cancel" }
]
})
);
看这个:不错。但为什么要将其存储在文档的数据中呢。为什么不仅仅是一个全局变量(或主作用域的局部变量)?@Joseph,我个人更喜欢
data()
而不是全局变量(主要是因为名称空间污染的原因),我不能保证提问者只有一个“主”作用域,所以…:)@弗里德里克:这看起来不错。非常感谢。
$(".another-dialog").dialog(
$.extend({}, $(document).data("common-dialog-options"), {
title: "Another dialog",
buttons: [
{ text: "OK" },
{ text: "Cancel" }
]
})
);
var defaultDialog = {
title: 'Another dialog',
autoOpen: false,
draggable: true,
modal: true,
show: 'fade',
hide: 'fade',
width: 400,
position: ['center', 'center'],
buttons: [
{ text: 'Ok' },
{ text: 'Cancel' }
],
open: function(event, ui) { $(".ui-dialog-titlebar-close span").html('×') }
};
var tunnedDialog= jQuery.extend(true, {}, defaultDialog );
tunnedDialog.title: 'Another dialog';
tunnedDialog.buttons: [
{ text: 'Ok' },
{ text: 'Cancel' }
]
$('.another-dialog').dialog(tunnedDialog);