Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 37 lines (29 sloc) 1.889 kb
1514ed1 @scottgonzalez Intro for Why Use the Widget Factory.
authored
1 # Why Use the Widget Factory
2
3 Writing jQuery plugins is as simple as adding a method to `jQuery.prototype`
4 (more commonly seen as `$.fn`) and following some simple conventions like returning `this` for chainability.
5 So why does the widget factory exist? And why is it hundreds of lines of code?
6
7 In this document, we'll walk through the benefits of the widget factory and find out
8 when and why it makes sense to use it.
9
10 ## Stateless vs. Stateful Plugins
11
12 Most jQuery plugins are stateless; they perform some action and their job is done.
13 For example, if you set the text of an element using `.text( "hello" )`,
14 there is no setup phase and the result is always the same.
15 For these types of plugins, it makes sense to just extend jQuery's prototype.
16
17 However, some plugins are stateful; they have full life cycles, maintain state, and react to changes.
18 These plugins require a lot of code dedicated to initialization and state management (and sometimes destruction).
19 This results in a lot of boilerplate for building stateful plugins.
20 Even worse, each plugin author may manage life cycles and state differently,
21 resulting in different API styles for differnet plugins.
22 The widget factory aims to solve both problems,
11d902a @scottgonzalez Added section on consistent API.
authored
23 removing the boilerplate and creating a consistent API across plugins.
24
25 ## Consistent API
26
27 The widget factory defines how to create and destroy widgets,
28 how to get and set options, how to invoke methods,
29 and how to listen to events triggered by the widget.
30 By using the widget factory to build your stateful plugins,
31 you're automatically conforming to a defined standard,
32 making it easier for new users to start using your plugins.
33 In addition to defining the interface,
34 the widget factory also implements much of this functionality for you.
35 If you're not familiar with the API provided by the widget factory,
36 you should read [How jQuery UI Works](how-jquery-ui-works.md).
Something went wrong with that request. Please try again.