gpt4 book ai didi

javascript - jQuery 插件与小部件

转载 作者:行者123 更新时间:2023-12-03 21:36:54 25 4
gpt4 key购买 nike

几个月前,我开始使用 jQuery 插件进行一些实验。我在互联网上找到了一些教程,然后我开始整理一些东西。
几天前,我需要构建自己的“插件”并回到我的旧项目。当我试图在互联网上找到更多信息时,我偶然发现了这些称为小部件的新“东西”。

据我了解,我应该忘记插件并开始开发小部件。这些插件是一个旧概念还是仍然有效?如果您需要带有界面的自定义 jquery 控件,最好的方法是什么?

我广泛使用 jQuery UI。

最佳答案

我自己也有一个类似的问题,我偶然发现了 this (jQuery 文档):

Stateless vs. Stateful Plugins

Most jQuery plugins are stateless; they perform some action and theirjob is done. For example, if you set the text of an element using.text( "hello" ), there is no setup phase and the result is alwaysthe same. For these types of plugins, it makes sense to just extendjQuery's prototype.

However, some plugins are stateful; they have full life cycles,maintain state, and react to changes. These plugins require a lot ofcode dedicated to initialization and state management (and sometimesdestruction). This results in a lot of boilerplate for buildingstateful plugins. Even worse, each plugin author may manage lifecycles and state differently, resulting in different API styles fordifferent 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 andset options, invoke methods, and listen to events triggered by thewidget. By using the widget factory to build your stateful plugins,you are automatically conforming to a defined standard, making iteasier for new users to start using your plugins. In addition todefining the interface, the widget factory also implements much ofthis functionality for you. (...)

Setting Options on Initialization

Whenever you build a plugin that accepts options, you should definedefaults for as many options as possible, then merge the user-providedoptions with the defaults on initialization. (...) The widget factoryprovides this functionality and even takes it a bit further.

我在这里发帖是为了提供功能引用,因为这是我首先寻找答案的地方! :)

关于javascript - jQuery 插件与小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6269051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com