jQuery plugins vs widgets
Asked Answered
S

2

25

Few months ago I started some experiments with jQuery plugin. I found some tutorials on the Interenet and I started to put something together.
A couple of days ago I had the need to build my own "plugin" and got back to my old project. Trying to find some more informations on the internet, I bumped in these new "things" called widgets.

For what I understand I should forget about plugins and start developing widgets. Are the plugins an old concept or are still valid? What is the best approach if you need a custom jquery control with interface?

I am using extensively jQuery UI.

Stedt answered 7/6, 2011 at 17:15 Comment(0)
O
25

You can either write a jQuery plugin or a jQuery UI widget.

Depends on what you want to do. Neither of these is outdated. If you write a widget, it will depend on jQuery UI (and jQuery of course), if you write a plugin, it will only depend on jQuery. Really, it depends on what you want to achieve.

Odonnell answered 7/6, 2011 at 17:19 Comment(2)
thanks for your answer. My confusion started when I read this article: erichynds.com/jquery/tips-for-developing-jquery-ui-widgets This guy has converted is "tool" into a widget from a plugin.Stedt
@Stedt I guess he made a decision that for depending on one more library, he will get some more nice ready functions, events and stuff. Might be easier to develop, but your plugin/widget can't be used if jQuery UI is not used for the project.Odonnell
C
28

I had an equivalent question my self and I stumble on this (jQuery Documentation):

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.

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. (...)

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. (...) The widget factory provides this functionality and even takes it a bit further.

I'm posting here for feature reference since this is where I'm first looking for an answer! :)

Chitkara answered 23/2, 2014 at 12:32 Comment(1)
That's much better... Better to quote than just posting links?!! or Both !!! I'll keep it in mind next time... :)Chitkara
O
25

You can either write a jQuery plugin or a jQuery UI widget.

Depends on what you want to do. Neither of these is outdated. If you write a widget, it will depend on jQuery UI (and jQuery of course), if you write a plugin, it will only depend on jQuery. Really, it depends on what you want to achieve.

Odonnell answered 7/6, 2011 at 17:19 Comment(2)
thanks for your answer. My confusion started when I read this article: erichynds.com/jquery/tips-for-developing-jquery-ui-widgets This guy has converted is "tool" into a widget from a plugin.Stedt
@Stedt I guess he made a decision that for depending on one more library, he will get some more nice ready functions, events and stuff. Might be easier to develop, but your plugin/widget can't be used if jQuery UI is not used for the project.Odonnell

© 2022 - 2024 — McMap. All rights reserved.