destroy
can.Control.prototype.destroy
Remove a Control from an element and clean up the Control.
control.destroy()
Prepares a control for garbage collection and is a place to reset any changes the control has made.
Remove a Control from an element and clean up the Control.
Prepares a control for garbage collection and is a place to reset any changes the control has made.
Allowing Garbage Collection
Destroy is called whenever a control's element is removed from the page using the library's standard HTML modifier methods. This means that you don't have to call destroy yourself and it will be called automatically when appropriate.
The following
Clicker
widget listens on the window for clicks and updates its element's innerHTML. If we remove the element, the window's event handler is removed auto-magically:The methods you can use that will destroy controls automatically by library:
jQuery and Zepto
Dojo
Mootools
YUI
Teardown in Destroy
Sometimes, you want to reset a controlled element back to its original state when the control is destroyed. Overwriting destroy lets you write teardown code of this manner. When overwriting destroy, make sure you call Control's base functionality.
The following example changes an element's text when the control is created and sets it back when the control is removed:
Base Functionality
Control prepares the control for garbage collection by: