jQuery 插件模板
1、为每一个DOM对象创建一个插件对象
模板定义:
(function($) {
$.pluginName = function(element, options) {
var defaults = {
foo: 'bar',
onFoo: function() {}
}
var plugin = this;
plugin.settings = {}
var $element = $(element),
element = element;
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
// code goes here
}
plugin.foo_public_method = function() {
// code goes here
}
var foo_private_method = function() {
// code goes here
}
plugin.init();
}
$.fn.pluginName = function(options) {
return this.each(function() {
//为每一个DOM元素创建插件实例
if (undefined == $(this).data('pluginName')) {
var plugin = new $.pluginName(this, options);
$(this).data('pluginName', plugin);
}
});
}
})(jQuery);
模板使用:
$(document).ready(function() {
// 将插件附加到选择器中的每一个元素(这里通过ID选择器,只有一个元素,下同。)
$('#element').pluginName({'foo': 'bar'});
// 隐式迭代,选择器中每一个元素都会调用该公共方法
$('#element').data('pluginName').foo_public_method();
// 隐式迭代,访问选择器中每一个元素的属性,返回一个数组
$('#element').data('pluginName').settings.foo;
});
2、面向对象的模板,只有一个插件实例
模板定义:
;(function($) {
$.pluginName = function(el, options) {
var defaults = {
propertyName: 'value',
onSomeEvent: function() {}
}
var plugin = this;
plugin.settings = {}
var init = function() {
plugin.settings = $.extend({}, defaults, options);
plugin.el = el;
// code goes here
}
plugin.foo_public_method = function() {
// code goes here
}
var foo_private_method = function() {
// code goes here
}
init();
}
})(jQuery);
模板使用:
$(document).ready(function() {
// 创建插件实例,并且绑定到$('#element')
var myplugin = new $.pluginName($('#element'));
// 调用插件公共方法
myplugin.foo_public_method();
// 获取公有属性的值
myplugin.settings.property;
});
3、面向对象的模板,充分利用习惯的链式编程
模板定义:
; (function ($) {
//构造函数
$.pluginName = function (el, options) {
var defaults = {
propertyName: 'value',
onSomeEvent: function () { }
}
var plugin = this;
plugin.settings = {}
var init = function () {
plugin.settings = $.extend({}, defaults, options);
plugin.el = el;
// code goes here
}
//①:直接方法定义(模板2就是采用这种方式)
plugin.foo_public_method = function () {
//公有方法
// code goes here
}
var foo_private_method = function () {
//私有方法
// code goes here
}
init();
}
//②:原型方法定义(均为公有方法)
$.pluginName.prototype = {
method1: function () {
},
method2: function () {
}
};
//③:原型方法定义 也可以这么写(均为公有方法)
//$.extend($.pluginName, {
// method1: function () {
// },
// method2: function () {
// }
//});
//在插件中使用
$.fn.pluginName = function (options) {
//实例化一个插件实例,通过执行构造函数初始化
var myPlugin = new $.pluginName(this, options);
//调用公有业务方法
myPlugin.method1();
myPlugin.foo_public_method();
//返回 this,便于链式调用
return this;
}
})(jQuery);
模板使用:
$(document).ready(function () {
//熟悉的链式编程
$('#element').pluginName({
//插件options
}).css({}).animate({});
});
4、构造函数提供给外部使用(有点别扭)
模板定义:
; (function ($) {
//构造函数
$.pluginName = function (el, options) {
//去除构造函数中对插件的初始化,转到$.fn.pluginName中初始化。
return $(el).pluginName(options);//该构造函数不是给插件使用,而是给外部调用者使用,需要return以链式编程
}
//②:原型方法定义(均为公有方法)
$.pluginName.prototype = {
method1: function (para1, para2) {
},
method2: function (para1, para2) {
}
};
//③:原型方法定义 也可以这么写(均为公有方法)
//$.extend($.pluginName, {
// method1: function (para1,para2) {
// },
// method2: function (para1,para2) {
// }
//});
//在插件中使用,不会创建插件实例(构造函数是给外部使用的)
$.fn.pluginName = function (options) {
var defaults = {
propertyName: 'value',
onSomeEvent: function () { }
}
var settings = {}
var init = function () {//私有方法
settings = $.extend({}, defaults, options);
// code goes here
}
var foo_private_method = function () {
//私有方法
// code goes here
}
init();
//调用业务方法
$.pluginName.method1(para1, para2);
$.pluginName.method2(para1, para2);
//返回 this,便于链式调用
return this;
}
})(jQuery);
模板使用:
$(document).ready(function () {
//熟悉的链式编程
$('#element').pluginName({
//插件options
}).css({}).show({});
//构造函数提供该外部使用,所以相当于
new $.pluginName($('#element'), {
//插件options
}).css({}).animate({});
});
总结
正如标题所说的那样,每种模板各有特点,但是最具可读性的还是第三种。当然了,模板只是一个套路,修修改改就又会变成另外一种模板了,上面只是总结了比较常见的模板格式,仅供参考。
不错的参考
jQuery 插件模板的更多相关文章
- 我最喜欢的jQuery插件模板
我使用jQuery已经有相当长的时间了,并且我会常常为它写一些插件(plugin).我尝试过用不同的方式去写,现在这个模板是我最喜欢的: 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...
- 关于jquery插件模板的两个案例
/** * jquery tips 提示插件 jquery.tips.js v0.1beta * * 使用方法 * $(selector).tips({ //selector 为jquery选择器 * ...
- JQuery插件模板
(function($){ $.fn.插件名 = function(settings){ var defaultSettings = { } /* 合并默认参数和用户自定义参数 */settings ...
- 简记 jQuery 插件模板
/** * @lisence jquery plugin demo v1.0.0 * * author: Jeremy Yu * * description: * this is a jquery p ...
- jQuery插件编写及链式编程模型小结
JQuery极大的提高了我们编写JavaScript的效率,让我们可以愉快的编写代码,做出各种特效.大多数情况下,我们都是使用别人开发的JQuery插件,今天我们就来看看如何把我们常用的功能做出JQu ...
- jQuery插件编写及链式编程模型
jQuery插件编写及链式编程模型小结 JQuery极大的提高了我们编写JavaScript的效率,让我们可以愉快的编写代码,做出各种特效.大多数情况下,我们都是使用别人开发的JQuery插件,今天我 ...
- 前端模板文件化jQuery插件 $.loadTemplates
工作中使用前端模板引擎,如 artTemplate.jsRender,来替代拼接字符串. 可是直接把模板写在页面上会带来页面臃肿,模板无法重用,与 ASP.NET等后端语言语法冲突等问题. 所以将多个 ...
- 出位的template.js 基于jquery的模板渲染插件
找了好几款基于jquery的模板渲染插件,无一感觉很难用(教程较少.绑定不统一),也可能我智商问题,比如jquery template.js .jtemplate.js. 然后在github上找到这一 ...
- javascript&&jquery编写插件模板
javascrpt插件编写模板 这里不分享如何编写插件,只留一个框架模板,使用面向对象的形式进行编写,方便管理 ;(function(window,document){ function FnName ...
随机推荐
- 【重叠I/O之系列三】I/O完成端口
一 串行模式和并行模式 一般一个服务应用程序采用以下两个架构模型之一: 串行模式 一个线程等待一个客户发出的请求,当请求到达的时候,线程会被换醒来处理客户的请求. 并发模式.一个线程等待一个客户 ...
- SQL语句与正则表达式
今儿个才知道SQL语句还可以搭配正则表达式作为查询条件,很是有用. REGEXP_LIKE(匹配)REGEXP_INSTR (包含)REGEXP_REPLACE(替换)REGEXP_SUBSTR(提取 ...
- 在页面生命周期执行时 Page 对象在 SaveState 阶段都发生了什么事?
先看下 SaveViewState 的源码: // Answer any state this control or its descendants want to save on freeze. / ...
- mac 查看系统时区
sudo systemsetup -gettimezone https://developer.apple.com/library/mac/documentation/Darwin/Reference ...
- GDI+中发生一般性错误(转载)
在开发.NET应用中,使用 System.Drawing.Image.Save 方法而导致“GDI+ 中发生一般性错误”的发生,通常有以下三种原因:1. 相应的帐户没有写权限.解决方法:赋予 NETW ...
- Requirejs开篇
前言 随着页面的内容丰富,以及网站体验更好.性能优化等,原有的通过script标签引入JavaScript脚本的方式已经不能很好地解决,此时新的一种JavaScript加载方式产生了--延时加载.执行 ...
- JavaScript解析json
后台数据经常以json数据格式传回前台,解析当然首选JSON对象. JSON对象有两个方法,使用JSON.parse(str)可以将json字符串解析成js中的对象. var o = JSON.par ...
- 九度OJ 1283 第一个只出现一次的字符
题目地址:http://ac.jobdu.com/problem.php?pid=1283 题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现 ...
- OpenJudge/Poj 1207 The 3n + 1 problem
1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...
- iis7.5 aspx,ashx的mime类型
映射aspx: 打开IIS管理器,找到“处理程序映射”,在列表右击选择“添加脚本映射”即可.eg:*.aspx,将该类型的页面的处理程序映射为“%windir%\Microsoft.NET\Frame ...