Details difference between jQuery plugin and Widget

巧了我就是萌 提交于 2021-01-29 08:32:22

问题


I found the difference between jQuery plugin and widget herelink. It states that "This is different from a standard jQuery plugin in two important ways. First, the context is an object, not a DOM element. Second, the context is always a single object, never a collection."

My question is : How does Widget hold the states? Here what does it mean by the context and single object?

Yes After reading books JQuery UI in Action I got answer of my question.

*"A widget’s differentiating feature is its concept of state. Many jQuery plugins don’t have—or need—the concept of state. Consider the following jQuery plugin that replaces the selected element’s contents with a random number:

$.fn.randomNumber = function() {
 return this.each( function( index, element ) {
 $( element ).html( Math.random() * 1000 );
 };
};

This plugin is designed to run once and be done. The plugin doesn’t remember which elements it changed or anything about them. Contrast that with any of the jQuery UI widgets, such as the dialog widget created in the following code:

<div id="dialog"></div>
<script>
 $( "#dialog" ).dialog({ title: "Hello World" });
</script>

The randomNumber() plugin knew nothing about the element it operated on; the dialog widget knows a whole lot about the element. It knows that it’s a dialog, that its title is "Hello World", and more. (We’ll look at how it remembers this information later in the chapter.) Dialog is an excellent candidate for a widget because it has a state to manage; you can open it, close it, change its title, change its height, and so forth. Conversely, the randomNumber() plugin isn’t a good widget candidate because it has no state. The general rule is this: use the widget factory when you want to build a plugin that maintains state."*

来源:https://stackoverflow.com/questions/62813110/details-difference-between-jquery-plugin-and-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!