Search code examples
javascriptjqueryjquery-uibrowser-historyhtml5-history

What is state management in jQuery?


In jQuery UI Widget Factory:

The jQuery UI Widget Factory is an extensible base on which all of jQuery UI's widgets are built. Using the widget factory to build a plugin provides conveniences for state management, as well as conventions for common tasks like exposing plugin methods and changing options after instantiation.

Could someone please explain in simple words (preferably with an example ), what does state management mean in this context? The state management I know is browser history state management (maybe using HTML5 History API, or history.js) but I don't see any relationship between it and jQuery UI Widget Factory purposes.

Your help is appreciated.


Solution

  • From "Why Use the Widget Factory?" section of documentations:

    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.