0

In "learning Jquery Third Edition" I found these lines:

Destroying widgets.... Calling .myWidget('destroy') will remove the myWidget widget from the page. The widget factory does most of the work, but if we have modified parts of the document inside _create (as we did here);so we need to clean up after ourselves.

However, when creating a new widget and I can't imagine a situation in which we don't modify _create function. Could you give me an example of doing so.

2 Answers 2

1

It's not saying if you've modified the _create method, it's saying if you've modified the document within the _create method (or any method for that matter).

The base destroy method does a number of things for you including:

  • Unbinds any events attached to the root element this.element that are namespaced under the widget.
  • Removes the instance from the elements jQuery data store.
  • Removes any classes that the widget factory might have appended during the widgets life cycle.

Anything above and beyond this would need to be cleaned up by the widget itself by extending the destroy method, e.g. removal of any elements appended to the document, unbinding any events bound to nodes other than this.element (you might have attached a resize or scroll event to the window or document etc.)

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

Comments

0

As above. The idea is that any DOM manipulation or events which are create/attached in the _create function are removed in the destroy function.

I've attached a modified example for a control I've created: http://pastebin.com/M9CzUajs

The link shows how attached events in particular are reverted in destroy and how any CSS modifications are also reapplied. This is quite common in progressive enchancement examples where the original element is hidden.

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.