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?的更多相关文章

  1. POJ 2947 Widget Factory(高斯消元)

    Description The widget factory produces several different kinds of widgets. Each widget is carefully ...

  2. 使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins)

    使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins) Note 这一章节的内容是基于 Scott Gonzalez 一篇博客 Building ...

  3. poj 2947 Widget Factory

    Widget Factory 题意:有n件装饰品,有m组信息.(1 <= n ,m<= 300)每组信息有开始的星期和结束的星期(是在mod 7范围内的)并且还包括num种装饰品的种类(1 ...

  4. Widget Factory (高斯消元解线性方程组)

    The widget factory produces several different kinds of widgets. Each widget is carefully built by a ...

  5. 软件公司的两种管理方式 总体来说,这个世界上存在两种不同的软件公司的组织结构。我把他们叫做 Widget Factory(小商品工厂) 和 Film Crews(电影工作组

    软件公司的两种管理方式 一个简单的回答应该是——“因为在我们的社会里,我们总是会认为薪水和会和职位的层次绑在一起”.但是,这个答案同时也折射出一个事实——我们的薪资是基于我们的所理解的价值,但这并没有 ...

  6. 通过扩展jQuery UI Widget Factory实现手动调整Accordion高度

    □ 实现Accordion高度一致 <head> <meta name="viewport" content="width=device-width&q ...

  7. 【POJ】2947 Widget Factory(高斯消元)

    http://poj.org/problem?id=2947 各种逗啊..还好1a了.. 题意我就不说了,百度一大把. 转换为mod的方程组,即 (x[1,1]*a[1])+(x[1,2]*a[2]) ...

  8. POJ Widget Factory 【求解模线性方程】

    传送门:http://poj.org/problem?id=2947 Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

  9. UVA 1564 - Widget Factory(高斯消元)

    UVA 1564 - Widget Factory 题目链接 题意:n种零件, 给定m个制作时间.每段时间制作k个零件,每种零件有一个制作时间,每段时间用Mon到Sun表示,求每一个零件的制作时间.还 ...

  10. Extending Widgets with the Widget Factory

    https://learn.jquery.com/jquery-ui/widget-factory/extending-widgets/ 实例 https://github.com/tkvw/jQue ...

随机推荐

  1. SpringBoot、ActiveMQ整合阿里大鱼-----短信服务

    3.短信微服务 3.1需求分析 构建一个通用的短信发送服务(独立于优乐选的单独工程),接收activeMQ的消息(MAP类型)  消息包括手机号(mobile).短信模板号(template_code ...

  2. Scrapy 教程(三)-网站解析

    有经验的人都知道,解析网站需要尝试,看看得到的数据是不是想要的,那么在scrapy中怎么尝试呢? 调试工具-shell 主要用于编写解析器 命令行进入shell scrapy shell url 这个 ...

  3. HNUSTOJ-1253 Babelfish(字典树)

    1253: Problem C: Babelfish 时间限制: 1 Sec  内存限制: 128 MB提交: 14  解决: 3[提交][状态][讨论版] 题目描述 Problem C: Babel ...

  4. 小程序中页面兼容h5标签的解析

    有时候当小程序向后台拿数据是一篇html标签的文章时,把它放进小程序会发现很多标签就不兼容,如果要一个个改又很麻烦,有没有方法可以很快地兼容html标签呢? 有个工具可以做到:wxParse 下载了它 ...

  5. MySql二进制版安装教程

    1.检查是否已安装过mariadb,若有便删除(linux系统自带的) [root@localhost /]# rpm -qa | grep mariadb [root@localhost /]# r ...

  6. visual studio 2013 生成依赖项关系图出错

    开始是说无法连接到sql服务器,我安装卸载localdb http://www.microsoft.com/zh-cn/download/details.aspx?id=29062 下载 CHS\x6 ...

  7. 管线命令(Pipe)

    管线命令接受|前面传来的stdout,管线示意图如下所示: 管线两个需要注意的地方: 1.管线仅会处理stdout,忽略对stderr的处理 2.管线必须接受前个指令的stdin才是 那么,如果我想接 ...

  8. 深度复数网络 Deep Complex Networks

    转自:https://www.jiqizhixin.com/articles/7b1646c4-f9ae-4d5f-aa38-a6e5b42ec475  (如有版权问题,请联系本人) 目前绝大多数深度 ...

  9. python笔记(3)---列表

    lst = ["bob","jack","马云","马化腾","王健林"] del lst[1:3] ...

  10. php内置函数分析range()

    PHP_FUNCTION(range) { zval *zlow, *zhigh, *zstep = NULL, tmp; , is_step_double = ; double step = 1.0 ...