Nikita
Nikita

Reputation: 6079

when is jQuery UI's destroy() called?

I get that when developing a jQuery UI widget one must override destroy and call super.destroy or destroy on any nested widgets. But when is/should destroy be called? Is it ever being called auto-magically by jQueryUI framework? Or should it be called by a client programmer (using my awesome widget)?

For example, say I have a lightbox (fancybox) that displays an accordion(). Say the accordion is being created in fancybox' onStart callback - as the lightbox is about to be displayed. Should accordion('destroy') be called in 'on-lightbox-close' callback? Seems unnecessary...

Upvotes: 1

Views: 5436

Answers (1)

JAAulde
JAAulde

Reputation: 19560

destroy is called by you when you want to destroy the widget.

It is also called by UI when you .remove() an element from the DOM which had been "widgetized."

As for your specific use case, it depends. Will the same lightbox instance be re-opened at any point in the future of that page-load's lifetime? If so, destroying would only add overhead as the next showing of the lightbox would require a "re-widgetization". If not, then not destroying would leave memory being taken up for no reason. (this assumes that the close of your lightbox does not cause the elements to be removed from the DOM)

Upvotes: 4

Related Questions