Why Use the Widget Factory?
https://learn.jquery.com/jquery-ui/widget-factory/why-use-the-widget-factory/
Writing jQuery plugins is as simple as adding a method to jQuery.prototype (more commonly seen as $.fn) and following some simple conventions like returning this for chainability.
So why does the widget factory exist? And why is it hundreds of lines of code?
In this document, we'll walk through the benefits of the widget factory and find out when and why it makes sense to use it.
这里涉及到的一个知识点是jQuery的链式调用
link Stateless vs. Stateful Plugins
Most jQuery plugins are stateless; they perform some action and their job is done. For example, if you set the text of an element using .text( "hello" ), there is no setup phase and the result is always the same. For these types of plugins, it makes sense to just extend jQuery's prototype.
However, some plugins are stateful; they have full life cycles, maintain state, and react to changes.
These plugins require a lot of code dedicated to initialization and state management (and sometimes destruction).
This results in a lot of boilerplate样板文件 for building stateful plugins. Even worse, each plugin author may manage life cycles and state differently, resulting in different API styles for different plugins.
The widget factory aims to solve both problems, removing the boilerplate and creating a consistent API across plugins.
link Consistent API
The widget factory defines how to create and destroy widgets, get and set options, invoke methods, and listen to events triggered by the widget.
By using the widget factory to build your stateful plugins, you are automatically conforming to a defined standard, making it easier for new users to start using your plugins.
In addition to defining the interface, the widget factory also implements much of this functionality for you.
If you're not familiar with the API provided by the widget factory, you should read How jQuery UI Works.
link Setting Options on Initialization
Whenever you build a plugin that accepts options, you should define defaults for as many options as possible, then merge the user-provided options with the defaults on initialization.
It's also a good idea to expose the defaults so that users can even change the default values.
A common pattern in jQuery plugins looks like this:
https://learn.jquery.com/plugins/basic-plugin-creation/ 使用原生的jQuery写插件
$.fn.plugin = function( options ) {
options = $.extend( {}, $.fn.plugin.defaults, options );
// Plugin logic goes here.
};
$.fn.plugin.defaults = {
param1: "foo",
param2: "bar",
param3: "baz"
};
The widget factory provides this functionality and even takes it a bit further. Let's see what this looks like with the widget factory.
$.widget( "ns.plugin", {
// Default options.
options: {
param1: "foo",
param2: "bar",
param3: "baz"
},
_create: function() {
// Options are already merged and stored in this.options
// Plugin logic goes here.
}
});
Why Use the Widget Factory?的更多相关文章
- POJ 2947 Widget Factory(高斯消元)
Description The widget factory produces several different kinds of widgets. Each widget is carefully ...
- 使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins)
使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins) Note 这一章节的内容是基于 Scott Gonzalez 一篇博客 Building ...
- poj 2947 Widget Factory
Widget Factory 题意:有n件装饰品,有m组信息.(1 <= n ,m<= 300)每组信息有开始的星期和结束的星期(是在mod 7范围内的)并且还包括num种装饰品的种类(1 ...
- Widget Factory (高斯消元解线性方程组)
The widget factory produces several different kinds of widgets. Each widget is carefully built by a ...
- 软件公司的两种管理方式 总体来说,这个世界上存在两种不同的软件公司的组织结构。我把他们叫做 Widget Factory(小商品工厂) 和 Film Crews(电影工作组
软件公司的两种管理方式 一个简单的回答应该是——“因为在我们的社会里,我们总是会认为薪水和会和职位的层次绑在一起”.但是,这个答案同时也折射出一个事实——我们的薪资是基于我们的所理解的价值,但这并没有 ...
- 通过扩展jQuery UI Widget Factory实现手动调整Accordion高度
□ 实现Accordion高度一致 <head> <meta name="viewport" content="width=device-width&q ...
- 【POJ】2947 Widget Factory(高斯消元)
http://poj.org/problem?id=2947 各种逗啊..还好1a了.. 题意我就不说了,百度一大把. 转换为mod的方程组,即 (x[1,1]*a[1])+(x[1,2]*a[2]) ...
- POJ Widget Factory 【求解模线性方程】
传送门:http://poj.org/problem?id=2947 Widget Factory Time Limit: 7000MS Memory Limit: 65536K Total Su ...
- UVA 1564 - Widget Factory(高斯消元)
UVA 1564 - Widget Factory 题目链接 题意:n种零件, 给定m个制作时间.每段时间制作k个零件,每种零件有一个制作时间,每段时间用Mon到Sun表示,求每一个零件的制作时间.还 ...
- Extending Widgets with the Widget Factory
https://learn.jquery.com/jquery-ui/widget-factory/extending-widgets/ 实例 https://github.com/tkvw/jQue ...
随机推荐
- 《剑指offer》面试题25 二叉树中和为某一值的路径 Java版
(判断是否有从根到叶子节点的路径,其和为给定值.记录这些路径.) 我的方法:这道题我是按照回溯的思路去做的,我们需要一个数据结构来保存和删除当前递归函数中添加的值.这里要打印一条路径,我们可以选择Li ...
- uboot启动第一阶段分析
一. uboot第一阶段初识 1.1. 什么是uboot第一阶段 1.1.1. 启动os三个阶段 1.1.1.1. bl0阶段 a. 这段代码是三星固化到iROM中,可以查看<S5PV210_i ...
- 洛谷P3366【模板】最小生成树-克鲁斯卡尔Kruskal算法详解附赠习题
链接 题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M&l ...
- CSS 毛玻璃效果
效果图: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <met ...
- 1、Java调用C语言(本地法)
这是一个比较麻烦的方法.. 一.首先,你要先安装一个VC,我的装在了D:\software\C++\Microsoft Visual Studio 二.我的jdk安装目录是D:\Program ...
- ASP.NET CORE 2.2 MVC 学习
百度云链接:https://pan.baidu.com/s/1_iSy3wq4Jegr6j_AH9nobA 提取码:n152
- ssm中web配置各框架的配置文件路径方式
一.在web文件中配置 使用逗号隔开 二.在applicationContext.xml文件中配置或引用 以下是引用方式 注: <import />标签要放在所有bean配置的最前面.
- 关于express 连接 mongodb数据库报错
关于express 连接 mongodb数据库报错 nodejs DeprecationWarning: current URL string parser is deprecated, and wi ...
- 关于代码手写UI,xib和StoryBoard
代码手写UI 这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用.Geek们喜欢用代码构建UI,是因为代码是键盘敲出来的,这样可以做到不开IB,手不离开键盘就完成工作,可以专注于编码环境, ...
- 18.Linux-CentOS系统根目录空间使用率100%问题?
问题描述:发现服务器根目录爆满100%? 排查步骤:1.先检查文件索引节点iNode使用率情况,[root@localhost ~]# df -hTi2.查看无用文件是否居多:[root@localh ...