如何从它的原型中销毁jQuery UI小部件(How to destroy a jQuery UI widgets from within it's prototype)

编程入门 行业动态 更新时间:2024-10-23 17:26:45
如何从它的原型中销毁jQuery UI小部件(How to destroy a jQuery UI widgets from within it's prototype)

我已经开始使用Widget Factory来创建插件了。 但是我似乎找不到从内部破坏插件的方法。

在迄今为止我发现的所有示例和教程中,它们都会从DOM中删除插件。 这一切都很好,但我也希望能够从我的原型中销毁插件。

例如,假设使用不正确的参数或不正确的元素调用插件,或者在一段时间后它不会验证。 我希望之后能够调用this.destroy()并销毁插件。 当我调用this.destroy()时,它会调用$ .Widget.prototype.destroy()和我后续的_destroy()方法,但实例仍在DOM元素上。 只有在调用$(el).pluginName('destroy')时才能真正销毁它。

这里的简单例子

说我们有一些HTML

<p>Lorem ipsum</p>

和我们的jQuery UI小部件,它旨在一旦创建就销毁它

$.widget('ns.test', { _create: function() { this.destroy(); }, _destroy: function() {} });

然后我们调用插头

$('p').test();

我希望实例不会在$('p')上设置,但确实如此

$('p').data(); >> Object {ns-test: $.widget.$.(anonymous function).(anonymous function)}

只有从DOM调用方法时才会被破坏

$('p').test('destroy'); >> Object {}

有人想过吗?

I've gotten on board to using the Widget Factory for creating plugins. However I can't seem to find a way to destroy the plugin from within.

In all the examples and tutorials I've found so far they destroy the plugin from the DOM. Which is all good and well but I would also like to be able to destroy the plugin from within my prototype.

Say for example the plugin is invoked with incorrect parameters or a incorrect element or after some time it does not validate. I would like to be able to call this.destroy() afterwards and destroy the plugin. When i call this.destroy() it does call the $.Widget.prototype.destroy() and my subsequent _destroy() method but the instance is still on the DOM element. Only when calling $(el).pluginName('destroy') does it get truly destroyed.

Quick example here

Say we have some html

<p>Lorem ipsum</p>

and our jQuery UI widget, which aims to destroy it as soon as it's created

$.widget('ns.test', { _create: function() { this.destroy(); }, _destroy: function() {} });

and we invoke the pluging so

$('p').test();

I would expect that the instance would not be set on the $('p') but it is

$('p').data(); >> Object {ns-test: $.widget.$.(anonymous function).(anonymous function)}

Only when calling the method from the DOM it gets destroyed

$('p').test('destroy'); >> Object {}

Any thoughts anyone?

最满意答案

+ blgt洞察力或多或少是正确的。 所以我正在添加它并接受它。

你可以从_create和_init以外的任何方法调用this.destroy() ,它会像你期望的那样工作,但我不完全确定原因。 可能与如何管理.data数据有关

+blgt insight was more or less correct. So I'm adding it and accepting it.

You can call this.destroy() from any method other than _create and _init and it will work as you expect, but I'm not entirely sure why. Probably has something to do with how the .data data is managed

更多推荐

本文发布于:2023-08-04 11:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415313.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原型   部件   jQuery   prototype   UI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!