我必须创建一个文档,为如何为大型站点编写jQuery插件提供“模型”。
例如:所有插件都应该:
$.fn.somePlugin = function() {
return this.each(function() {
// Here the plugins does its goal.
});
};
因此他们尊重流畅的模型,同时也可以使用多个元素调用它们。我认为他们应该拥有的其他一些东西是:
你的“模型插件”怎么样? (以最好的方式实现这个以及你认为必要的其他一些事情)。
结果
Here您可以根据我阅读的所有信息查看我的插件模板。
答案 0 :(得分:5)
jquery文档有一个关于插件创作的部分: http://docs.jquery.com/Plugins/Authoring
这是来自本·阿尔曼斯的“幻灯片”谈论波士顿jquery会议的插件创作:
https://github.com/cowboy/talks/blob/master/jquery-plugin-authoring.js
还有另一个来自本·阿尔曼的关于编写插件的链接。
答案 1 :(得分:0)
我通常使用类似于
的结构(function ($, plugin) {
"use strict";
$[plugin] = function (options/* params */) {
var settings;
settings = $.extend({}, $[plugin].defaultSettings, options);
//static funciton code here
};
$.fn[plugin] = function (options/* params */) {
this.each(function (index, element) {
var settings, $this;
settings = $.extend({}, $.fn[plugin].defaultSettings, options);
$this = $(this);
$this.data(plugin+'Settings', settings);
//chainable function code here
});
return this;
};
$[plugin].defaultSettings = {
'foo': 'bar'
};
$.fn[plugin].defaultSettings = {
'fizz': 'buzz'
};
$(function(){
//document.ready initialization code here
});
}(jQuery, 'foo'));
我通常不打扰plugin
参数,但它可用于概括插件的名称
对于事件快捷方式,我将使用:
$.each('foo bar baz'.split(' '), function(i, name) {
$.fn[name] = function(data,fn){
if (fn == null) {
fn = data;
data = null;
}
return arguments.length > 0 ?
this.bind(name, data, fn) :
this.trigger(name);
};
});
这会产生.foo()
,.bar()
,.baz()
作为绑定/触发'foo'
,'bar'
和'baz'
事件的快捷方式
答案 2 :(得分:0)
我现在已经使用以下模板很长时间了,它似乎做了所需的一切,并提供了传统的jQuery脚本,例如:$.myPlugin("element", {options})
,$.myPlugin({options}, callback)
或'$ ( “元件”)为myplugin();
(function($) {
if (!$.myExample) { // check your plugin namespace does not already exist
$.extend({ // this will allow you to add your plugin to the jQuery lib
myExample: function(elm, command, args) {
// keep in mind, right here you might want to do a class or data check to determine which direction this call is going
// for example, upon init the plugin on an element you may add the plugin name as a class,
// this way, when it's recalled, you can see it alrady has that class and might be calling a command,
// thus make an if statemnt to push the process through
return elm.each(function(index){
// do work to each element as its passed through
// be sure to use something like
// return elm.each(function(e) { dor work });
// as your final statement in order to maintain "chainability"
});
}
});
$.fn.extend({ // this gives the chainability functionality seen with $ funcs like: $("#eleID").css("color", "red") <--returns original element object
myExample: function(command) {
return $.myExample($(this), command, Array.prototype.slice.call(arguments, 1));
}
});
$.myExample.props = { // Here you can establish specific properties to your plugin, prehaps even make them "Over-writable"
key1: "value",
key2: "value"
};
$.myExample.methods = { // Here you can establish specific methods/functions for your plguin to carry out and maintain your namespace as well
key1: function(param) {
/* do work */
},
key2: function(param) {
/* do work */
}
};
// This next part is not seen in many plugins but useful depending on what you're creating
$.myExample.init = function(param) { // If you have an initialize method to apply, namespace it in here and calll on initializing your plugin
var key = "value",
key2 = {
subKey: "value"
};
/*
/ run any number of initializing functions here
/ I prefer to make my param a value that can be a
/ string with a possible object
/ the string for holding a base configuration
/ the object for any change in properties or base values for that config
*/
};
$.myExample.defaults = { // establish base properties here that can be over-written via .props, but their values should never truly change
key1: "value",
key2: {
prop1: {
subKey1: "value",
subKey2: "value"
},
prop2: {
subKey1: "value"
}
},
key3: function(param) {
}
};
}
})(jQuery);