[英]How to pass a jQuery UI widget into a function?
I have two functions which perform the same operations against two different widget types. 我有两个函数针对两种不同的小部件类型执行相同的操作。 I thought I had figured out an implementation which would allow me to use one generic function instead of two.
我以为我想出了一个实现,可以让我使用一个泛型函数而不是两个泛型函数。 The idea was that I would pass each type of widget into this generic function.
我的想法是,我会将每种类型的小部件都传递给该泛型函数。 However, I am struggling with the details:
但是,我在细节上苦苦挣扎:
Here are my two functions which repeat a lot of their code: 这是我的两个函数,它们重复了很多代码:
buildTemplateLookupTreeDialog: function () {
lookupTreeViewDialog.bind('loaded.jstree', function () {
var templateID = workflowDialogContent.find('#DeviceTemplateID').val();
lookupTreeViewDialog.TemplateLookupTree('setSelectedNode', '#' + templateID);
lookupTreeViewDialog.TemplateLookupTree('saveCookie');
}).TemplateLookupTree();
lookupTreeViewDialog.TemplateLookupTree('onNodeDblClick', function (template) {
if (openDialog != null) {
openDialog.TemplateLookupTree('saveCookie');
openDialog.data('result', { id: template.id, name: template.name });
openDialog.dialog('close');
}
});
},
buildComponentLookupTreeDialog: function () {
lookupTreeViewDialog.bind('loaded.jstree', function () {
var componentID = workflowDialogContent.find('#DeviceComponentID').val();
lookupTreeViewDialog.ComponentLookupTree('setSelectedNode', '#' + componentID);
lookupTreeViewDialog.ComponentLookupTree('saveCookie');
}).ComponentLookupTree();
lookupTreeViewDialog.ComponentLookupTree('onNodeDblClick', function (component) {
if (openDialog != null) {
openDialog.ComponentLookupTree('saveCookie');
openDialog.data('result', { id: component.id, name: component.name });
openDialog.dialog('close');
}
});
}
and here is what I thought a generic implementation could look like: 这就是我认为通用实现的样子:
buildGenericLookupTreeDialog: function (lookupTreeWidget, title, idSelector, nameSelector) {
lookupTreeViewDialog.bind('loaded.jstree', function () {
var selectedNodeID = workflowDialogContent.find(idSelector).val();
lookupTreeViewDialog.lookupTreeWidget('setSelectedNode', '#' + selectedNodeID);
lookupTreeViewDialog.lookupTreeWidget('saveCookie');
}).lookupTreeWidget();
lookupTreeWidget('onNodeDblClick', function (node) {
if (openDialog != null) {
lookupTreeWidget('saveCookie');
openDialog.data('result', { id: node.id, name: node.name });
openDialog.dialog('close');
}
});
}
//Called like so
this.buildGenericLookupTreeDialog(TemplateLookupTree, 'Select Template', '#DeviceTemplateID', '#DeviceTemplateName');
Now, this code immediately throws an error --TemplateLookupTree not defined. 现在,此代码立即引发未定义的错误--TemplateLookupTree。 It appears that I can't reference the widget by its name unless I am actually initializing it (eg lookupTreeViewDialog.TemplateLookupTree());
看来我无法按其名称引用该窗口小部件,除非我实际对其进行了初始化(例如,lookupTreeViewDialog.TemplateLookupTree());
If I initialize TemplateLookupTree before passing into buildGenericLookupTree -- I receive errors whenever lookupTreeWidget is used liked a function. 如果在传递给buildGenericLookupTree之前初始化TemplateLookupTree,则每当使用lookupTreeWidget时,都会收到错误消息,就像函数一样。
Any suggestions on how to DRY this code? 关于如何干燥此代码的任何建议?
将查找树窗口小部件名称作为字符串传递,并检出此线程以获取有关如何调用它的信息: 当我将其名称作为字符串时,如何执行JavaScript函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.