我的 jQuery 插件有弱点(模式方面)吗?
我在网上找到了一个插件模式。 (我一找到它就会提交链接)并且我对其进行了一些修改以创建我自己的对话框插件。恐怕即使代码可以工作,我的制作方式也没有意义。
我必须:
- 能够将我的插件分配给 多个元素(多于一个 对话框)-已实现
- 我必须能够访问它的方法 :打开对话框,关闭对话框, 从其范围之外分配选项 -尚未实施
- 我想发送参考 当有按钮时当前对话框 发生点击 - 部分实现。 我正在使用
$(this).getDialog()
方法。this
指的是点击的 按钮
这是插件 :
(function($) {
var pluginName = "Dialog",
dialogContent = { "Title" : "h2", "SubTitle" : "p.sub-title", "Body" : "div.content" }
var infDialog = function( el, options ) {
var $el = $(el),
currentConfig = {
position : [100, 100],
autoOpen : true
};
$el.data( pluginName, $el);
if ( options ) {
currentConfig = $.extend( currentConfig, options );
}
$el.css({ "top" : currentConfig.position[1], "left" : currentConfig.position[0], "z-index" : 9999 });
if ( currentConfig.autoOpen ) {
$el.fadeIn( "slow" );
}
if ( currentConfig.buttons ) {
$.each(currentConfig.buttons, function(i, j) {
if ( $.isFunction( j ) && $(el).find("input[value='" + i + "']").length )
{
var $currentButton = $(el).find("input[value='" + i + "']");
$(el).find("input[value='" + i + "']").click(function() {
j.call( $currentButton );
});
}
});
}
if ( currentConfig.onOpen ) {
currentConfig.onOpen.call( $el );
}
if ( currentConfig.onClose ) {
$el.bind("onclose", function() {
currentConfig.onClose.call( $el );
});
}
$el.getDialog().bind("click", function( e ) {
var currentDialog = this.id,
currentPosition = $(this).css("z-index");
if ( currentPosition < 9999 || currentPosition == "auto" ) {
$(".dialog").each(function(i) {
if ( this.id == currentDialog ) {
$(this).css("z-index", 9999);
} else {
$(this).css("z-index", 9990);
}
});
$(this).css("z-index");
}
});
}
$.fn.infDialog = function( options ) {
return this.each(function() {
( new infDialog( this, options ) );
});
}
$.fn.closeDialog = function() {
return $(this).getDialog().fadeOut("slow", function() {
$(this).trigger("onclose");
});
}
$.fn.getDialog = function() {
return ( ! $(this).is(".dialog") ) ? $(this).closest(".dialog") : $(this);
}
$.fn.assignOption = function( options ) {
var $currentPlugin = $(this);
$.each( options, function(i, j) {
if ( dialogContent[ i ] ) {
$currentPlugin.find( dialogContent[ i ] ).empty().html( j );
}
});
}
})(jQuery);
和对话框的 HTML :
<div id="dialogTest" class="dialog">
<div>
<h2>title</h2>
<p class="sub-title">
subtitle
</p>
<div class="content">
Content
</div>
<p class="buttons"><input type="button" value="Action" /> <input type="button" value="Close" class="normal" /></p>
</div>
</div>
以及 jQuery 代码 :
$("#dialogTest").infDialog({
position : [400, 190],
buttons : {
"Action" : function() {
var $dialog = $(this).getDialog(),
obj = $dialog.data("Dialog"),
$currentDialog = $(this).getDialog();
$currentDialog.assignOption({
"Title" : "New Title",
"SubTitle" : "Lorem ipsum",
"Bob" : "unknown body tag",
"Body" : "testbody"
});
$(this).attr("value", Math.random());
},
"Close" : function() {
$(this).closeDialog();
}
},
onOpen : function() {
},
onClose : function() {
var $currentDialog = $(this).getDialog();
$currentDialog.fadeIn("fast");
}
});
我做错了什么或者我实际上正在朝着好的方向前进?
顺便说一句,我发现设计模式建议的这段代码: $el.data(pluginName, $el);
不起作用。事实上,每次我尝试使用 $("#dialogTest").data("Dialog")
检索对象时,返回的对象都是空的。
谢谢
I found a plugin pattern on Internet. ( I will submit the link as soon as I find it back ) and I modified it a bit to create my own dialog plugin. I'm afraid even if the code is working, the way I made it doesn't make sense.
I must :
- Be able to assign my plugin to
multiple element (more then one
dialog) -Implemented - I must be able to access it's method
: openDialog, closeDialog,
assignOptions from outside it's scope
-Not yet implemented - I would like to send a reference to
the current dialog when a button
click occurs - Partially implemented.
I'm using$(this).getDialog()
method.this
refer to the clicked
button
Here is the plugin :
(function($) {
var pluginName = "Dialog",
dialogContent = { "Title" : "h2", "SubTitle" : "p.sub-title", "Body" : "div.content" }
var infDialog = function( el, options ) {
var $el = $(el),
currentConfig = {
position : [100, 100],
autoOpen : true
};
$el.data( pluginName, $el);
if ( options ) {
currentConfig = $.extend( currentConfig, options );
}
$el.css({ "top" : currentConfig.position[1], "left" : currentConfig.position[0], "z-index" : 9999 });
if ( currentConfig.autoOpen ) {
$el.fadeIn( "slow" );
}
if ( currentConfig.buttons ) {
$.each(currentConfig.buttons, function(i, j) {
if ( $.isFunction( j ) && $(el).find("input[value='" + i + "']").length )
{
var $currentButton = $(el).find("input[value='" + i + "']");
$(el).find("input[value='" + i + "']").click(function() {
j.call( $currentButton );
});
}
});
}
if ( currentConfig.onOpen ) {
currentConfig.onOpen.call( $el );
}
if ( currentConfig.onClose ) {
$el.bind("onclose", function() {
currentConfig.onClose.call( $el );
});
}
$el.getDialog().bind("click", function( e ) {
var currentDialog = this.id,
currentPosition = $(this).css("z-index");
if ( currentPosition < 9999 || currentPosition == "auto" ) {
$(".dialog").each(function(i) {
if ( this.id == currentDialog ) {
$(this).css("z-index", 9999);
} else {
$(this).css("z-index", 9990);
}
});
$(this).css("z-index");
}
});
}
$.fn.infDialog = function( options ) {
return this.each(function() {
( new infDialog( this, options ) );
});
}
$.fn.closeDialog = function() {
return $(this).getDialog().fadeOut("slow", function() {
$(this).trigger("onclose");
});
}
$.fn.getDialog = function() {
return ( ! $(this).is(".dialog") ) ? $(this).closest(".dialog") : $(this);
}
$.fn.assignOption = function( options ) {
var $currentPlugin = $(this);
$.each( options, function(i, j) {
if ( dialogContent[ i ] ) {
$currentPlugin.find( dialogContent[ i ] ).empty().html( j );
}
});
}
})(jQuery);
and the HTML of a dialog :
<div id="dialogTest" class="dialog">
<div>
<h2>title</h2>
<p class="sub-title">
subtitle
</p>
<div class="content">
Content
</div>
<p class="buttons"><input type="button" value="Action" /> <input type="button" value="Close" class="normal" /></p>
</div>
</div>
and the jQuery code :
$("#dialogTest").infDialog({
position : [400, 190],
buttons : {
"Action" : function() {
var $dialog = $(this).getDialog(),
obj = $dialog.data("Dialog"),
$currentDialog = $(this).getDialog();
$currentDialog.assignOption({
"Title" : "New Title",
"SubTitle" : "Lorem ipsum",
"Bob" : "unknown body tag",
"Body" : "testbody"
});
$(this).attr("value", Math.random());
},
"Close" : function() {
$(this).closeDialog();
}
},
onOpen : function() {
},
onClose : function() {
var $currentDialog = $(this).getDialog();
$currentDialog.fadeIn("fast");
}
});
I am making something wrong or I'm actually heading the good way?
On a side note, I found that this code : $el.data( pluginName, $el);
suggested by the design pattern doesn't work. In fact, every time I tried to retreive the object using $("#dialogTest").data("Dialog")
, the returned object was empty.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 插件创作教程 为我提供了我使用的 jQuery 插件模式。
关于您的
assignOptions
任务...如果您有一个私有设置对象并向您的插件传递选项,它会很好地工作(当然,这在教程中进行了概述)。
扩展示例
The jQuery Plugin Authoring tutorial gave me the jQuery plugin pattern I use.
Regarding your
assignOptions
task...if you have a private settings object and pass your plugin the options it works nicely (this is outlined in the tutorial of course).
Example of extending
一些快速提示:
在分配给
jquery.fn
(jQuery 原型)的函数表达式中,this
已经是一个 jQuery 对象。一个插件在
jquery.fn
命名空间上使用 4 个方法名称通常不是一个好主意。使用 jQuery UI Widget 框架,您只会使用一个方法名称然后
您可以在不依赖 jquery.ui.widget.js 的情况下执行此操作或类似操作
on
前缀对于大多数 jQuery 事件来说是不常见的A couple of quick tips for you:
In a function expression that is assigned to the
jquery.fn
(The jQuery Prototype),this
is already a jQuery object.It's not usually a good idea for one plugin to use 4 method names on the
jquery.fn
namespace. Using the jQuery UI Widget framework, you would only use one method nameThen
You'd could do this or something similar with out the dependency on jquery.ui.widget.js
The
on
prefix is unusual for most jQuery events