0

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?

2
  • 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 Commented Apr 2, 2015 at 10:41
  • Thanks! I tried it out and it and did some more testing but it seems that as long as the widget is the 'initiator' it won't work. Calling this._trigger for instance won't work either unless the triggering is done from outside, from within a window.setTimeout for example. Using this.destroy() elsewhere didn't work either unless it was initiated through a user action like a click or hover. Guess I'll have to live with it. Thanks anyways Commented Apr 2, 2015 at 11:35

1 Answer 1

0

+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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.