In a typical Knockout application, DOM elements are dynamically added and removed, for example using the template binding or via control-flow bindings (if, ifnot, with, and foreach). When creating a custom binding, it is often desirable to add clean-up logic that runs when an element associated with your custom binding is removed by Knockout.
Registering a callback on the disposal of an element
To register a function to run when a node is removed, you can call ko.utils.domNodeDisposal.addDispos