编写jquery插件的模型
我必须创建一个文档,为如何为大型站点编写 jQuery 插件提供“模型”。
例如:所有插件都应该具有:
$.fn.somePlugin = function() {
return this.each(function() {
// Here the plugins does its goal.
});
};
所以它们尊重流畅模型,并且可以同时使用多个元素调用。我认为它们都应该具有的其他一些东西是:
- 选项 getter 和 setter(如 jQuery-ui 中)
- 方法(如 jQuery-ui 中)
- 更改默认选项的某种方法。当然,这应该在不修改插件文件的情况下完成(同样,像 jQuery-ui 一样)。
你的“模型插件”会怎样? (以尽可能最好的方式实现这一点以及您认为必要的其他一些事情)。
结果
在这里你可以看到我基于所有信息的插件模板我读了。
I have to create a document that provides a "model" for how to write jQuery plugins for a large site.
For example: all plugins should have:
$.fn.somePlugin = function() {
return this.each(function() {
// Here the plugins does its goal.
});
};
so they respect the fluent model and also they can be called with multiple elements at same time. Some other things I think they should all have are:
- Options getter and setter (like in jQuery-ui)
- Methods (like in jQuery-ui)
- Some way to change default options. Of course this should be done without modifying the plugin file (again, like jQuery-ui).
How would it be your "Model Plugin"? (achieving this and some other things you think necessary in the best possible way).
Result
Here you can see my plugin template based on all the information I read.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jquery 文档有一个关于插件创作的部分:
http://docs.jquery.com/Plugins/Authoring
这是本的“幻灯片” almans 在波士顿 jquery 会议上谈论插件创作:
https://github.com/cowboy/talks/blob/master/jquery-plugin-authoring.js
以及 ben alman 提供的有关编写插件的另一个链接。
http://msdn.microsoft.com/en-us/scriptjunkie/ff696759
The jquery docs have a section on plugin authoring:
http://docs.jquery.com/Plugins/Authoring
and here's the "slides" from ben almans talk on plugin authoring from the boston jquery conference:
https://github.com/cowboy/talks/blob/master/jquery-plugin-authoring.js
and one more link from ben alman about writing plugins.
http://msdn.microsoft.com/en-us/scriptjunkie/ff696759
我通常使用与此类似的结构,
我通常不会打扰
plugin
参数,但它对于概括插件的名称可能很有用对于事件快捷方式,我将使用
:
.foo()
、.bar()
、.baz()
都是绑定/触发'foo'< 的快捷方式/code>,
'酒吧'
,和'baz'
事件。I typically use a structure similar to this
I don't usually bother with the
plugin
parameter, but it could be useful for generalizing the name of a pluginFor event shortcuts, I'll use:
Which will produce
.foo()
,.bar()
,.baz()
all as shortcuts for binding/triggering the'foo'
,'bar'
, and'baz'
events.我已经使用下面的模板很长时间了,它似乎可以完成所需的一切,并提供传统的 jQuery 脚本,例如: $.myPlugin("element", {options}) ,
$.myPlugin({options},callback)
, 或 '$("element").myPlugin();I've been using the following template for a LONG time now and it seems to do everything needed as well as provide for traditional jQuery scripting, such as:
$.myPlugin("element", {options})
,$.myPlugin({options}, callback)
, or '$("element").myPlugin();