jQuery UI Why Use the Widget Factory |
-- Learning not just technology, but dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
jQuery UI Tutorial
- jQuery UI Tutorial
- jQuery UI Introduction
- jQuery UI Download
- jQuery UI Usage
- jQuery UI Customization
- jQuery UI How it Works
jQuery UI Themes
jQuery UI Widget Factory
- jQuery UI Widget Factory
- jQuery UI Extending Widgets with the Widget Factory
- jQuery UI Widget Method Invocation
- jQuery UI Why Use the Widget Factory
- jQuery UI How to Use the Widget Factory
jQuery UI Reference
- jQuery UI API Documentation
- API Category β Effects
- API Category β Effects Core
- API Category β Interactions
- API Category β Method Overrides
- API Category β Methods
- API Category β Selectors
- API Category β Theming
- API Category β UI Core
- API Category β Utilities
- API Category β Widgets
jQuery UI Examples
- jQuery UI Examples
- Draggable
- Droppable
- Resizable
- Selectable
- Sortable
- Accordion
- Autocomplete
- Button
- Datepicker
- Dialog
- Menu
- Progressbar
- Slider
- Spinner
- Tabs
- Tooltip
- Effect
- Show
- Hide
- Toggle
- .addClass()
- .removeClass()
- .toggleClass()
- .switchClass()
- Color Animation
- Position
- Widget Factory
jQuery UI Widget Method Invocation
jQuery UI How to Use the Widget Factory
Deep Dive
- Scripting Languages
- Web Services
- Network Services
- Programming Languages
- Software
- Programming
- Web Design & Development
- Scripting
- Computer Science
- Development Tools
jQuery UI Why Use the Widget Factory
Writing a jQuery plugin is as simple as adding a method to jQuery.prototype (commonly shown as $.fn), and requires following a few simple rules, like returning this. So why does the Widget Factory exist?
In this chapter, we will explain the benefits of the Widget Factory and understand when and why to use it.
Stateless vs. Stateful Plugins
Most jQuery plugins are stateless; they perform some action and their job is done. For example, if you set an element's text with .text( "hello" ), there is no setup phase, and the result is always the same. For this type of plugin, it simply extends jQuery's prototype.
However, some plugins are stateful; they have a full lifecycle, maintain state, and react to changes. These plugins require a lot of boilerplate code for initialization and state management (and sometimes destruction). This has led to the creation of templates for creating stateful plugins. Worse, each plugin author manages the lifecycle and state of their plugins in different ways, leading to different API styles for different plugins. The Widget Factory aims to solve these problems by removing the boilerplate and creating a consistent API for plugins.
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 create stateful plugins, they automatically conform to the defined standards, making it easier for new users to use your plugin. Additionally, the Widget Factory enables the definition of interfaces. If you are not familiar with the API provided by the Widget Factory, please see How to Use the Widget Factory.
Setting Options at Initialization
When you create a plugin that accepts options, you should define defaults for as many options as possible. Then, during initialization, merge the user-provided options with the defaults. You can also expose the defaults so users can change the default values. A common pattern in jQuery plugins is shown below:
$.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 also provides this functionality and improves upon it. Using the Widget Factory, it would look like this:
$.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.
}
});
jQuery UI Widget Method Invocation
jQuery UI How to Use the Widget Factory
Click to Share Notes
Write notes...
Image URL:
Image Description:
Image Size: Γ
Nickname: (Required)
Email: (Required)
Reference URL:
YouTip