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. } });
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2015-06-24 How does it work in C#? - Part 3 (C# LINQ in detail)
2015-06-24 Counting sheep...
2015-06-24 Convert boolean values to strings 'Yes' or 'No'.
2015-06-24 Function 1 - hello world
2015-06-24 Return Negative
2015-06-24 CodeWars题目筛选
2015-06-24 Miles per gallon to kilometers per liter