调用传递给JavaScript/jQuery中函数的配置对象的方法
我有一个函数,它接受一个配置对象,然后生成一个带有jQuery UI的模式对话框,如下所示:调用传递给JavaScript/jQuery中函数的配置对象的方法,javascript,jquery,function,object,modal-dialog,Javascript,Jquery,Function,Object,Modal Dialog,我有一个函数,它接受一个配置对象,然后生成一个带有jQuery UI的模式对话框,如下所示: function modalDialog(settings) { var options = $.extend({ selector: '.dialog', // the dialog element selector disabled: false, // d
function modalDialog(settings) {
var options = $.extend({
selector: '.dialog', // the dialog element selector
disabled: false, // disables(true) or enables (false) the dialog
autoOpen: false, // dialog opens automatically when called (true), (false) dialog hidden until .dialog(open) method called
closeOnEscape: true, // dialog closes when focused and ESC key pressed
closeText: 'close', // specifies text for close button
draggable: false, // (true) dialog draggable by the title bar
resizable: false, // dialog is resizable
height: 'auto', // height of dialog in px
minHeight: false, // min-height in px
maxHeight: false, // max-height in px
width: 300, // width of dialog in px
minWidth: 150, // min-width in px
maxWidth: false, // max-width in px
modal: true, // disables other items on page
hide: null, // the effect to be used when dialog is closed
show: null, // the effect to be used when dialog is opened
position: 'center', // specifies where dialog should be displayed: single string, array of co-ordiantes, array of string values
title: '', // dialog title. Any valid HTML may be used
zIndex: 1000, // starting z-index for the dialog
stack: true // specifies if dialogs will stack on other dialogs
}, settings || {});
$(options.selector).dialog({
disabled: options.disabled,
autoOpen: options.autoOpen,
closeOnEscape: options.closeOnEscape,
closeText: options.closeText,
draggable: options.draggable,
resizable: options.resizable,
height: options.height,
minHeight: options.minHeight,
maxHeight: options.maxHeight,
width: options.width,
minWidth: options.minWidth,
maxWidth: options.maxWidth,
modal: options.modal,
hide: options.hide,
show: options.show,
position: options.position,
title: options.title,
zIndex: options.zIndex,
stack: options.stack,
create: function(event, ui){
if (typeof options.createCall == 'function') {
options.createCall.call(this);
}
},
open: function(event, ui){
if (typeof options.openCall == 'function') {
options.openCall.call(this);
}
},
beforeClose: function(event, ui){
if (typeof options.beforeCloseCall == 'function') {
options.beforeCloseCall.call(this);
}
},
close: function(event, ui){
if (typeof options.closeCall == 'function') {
options.closeCall.call(this);
}
},
focus: function(event, ui){
if (typeof options.focusCall == 'function') {
options.focusCall.call(this);
}
}
});
}
我正在处理的项目中可能会有很多模态,因此我认为将配置对象存储在对象文本中而不是动态生成它们会更整洁。大概是这样的:
icisSite.modalStore = {
tradeFlowGraph: {
selector: '.rtx-TradeFlowGraphs',
title: 'Fertilizer Trade Flow graphs',
width: 800,
openCall: function(){
carouselLink
.eq(0)
.trigger('click');
}
}
}
然后,可以通过将引用传递到存储对象来创建模态:
modalDialog(icisSite.modalStore.tradeFlowGraph);
我遇到的问题是,当以这种方式传递给modalDialog函数时,openCall方法没有被调用。当像这样传递配置对象时,它确实起作用,我不知道为什么:
modalDialog({
selector: '.rtx-TradeFlowGraphs',
title: 'Fertilizer Trade Flow graphs',
width: 800,
openCall: function(){
carouselLink
.eq(0)
.trigger('click');
}
});
虽然像这样传递参数不是问题,但最好将它们集中存储在一个随时可用的对象文本中,而不是创建和传递临时对象。似乎将icissisite.modalStore对象文本引入jQuery范围可以解决问题 将其包装到工厂函数中,如下所示: $(函数(){ ICISite.modalStore={ 交易流程图:{
选择器:'.rtx贸易流程图',
标题:“肥料贸易流量图”, 宽度:800,
openCall:function(){ 旋转木马 .eq(0) .trigger('click');}})
只是
openCall()
函数不起作用吗?也就是说,选择器
、标题
等是否都能按预期工作?这应该可以正常工作(所有内容的拼写都正常),所以我猜还有一些事情我们没有代码。@Ken Redler-是的,是open call函数不起作用。