kmalakoff/knockback

Enhancement - Custom ViewModel dispose callbacks

Closed this issue · 3 comments

Hi Kevin,

I think it would be nice to have the ability to define a custom "dispose" callback in the ViewModel which would allow us to clear things like timeouts or intervals.

Currently, if I add a custom "dispose" callback to the ViewModel function prototype, "kb.release" will invoke it but will skip the rest of the ViewModel keys:

…
} else if (typeof obj.dispose === 'function') {
   obj.dispose();
} else if (!ko.isObservable(obj)) {
   this.releaseKeys(obj);
}
…

Thoughts?

Thanks,
Juan Cortines

By the way, the current workaround I found is to use "ko.utils.domNodeDisposal.addDisposeCallback" inside a ko custom binding.

Yes, this is by design. The idea is that if you write a custom release, dispose, or destroy method, you are taking control over the lifecycle.

Inside you custom lifecycle method, call kb.releaseKeys(this).

Thanks, now is clear :)