Reputation: 10509
I am using knockoutJS as a client-side MVVM framework.
Sometimes I am creating a temporary view models (via js functions) and assign them to DOM elements that are dynamically loaded.
When, for example, a comments panel for some content is no longer needed, I remove the comments panel div from the DOM. What happens to the variable that was used as a view model when I called applyBindings with specifying a DOM element parameter? It is being somehow disposed? Or am I responsible to handle that? If so - how do I do this?
Upvotes: 5
Views: 854
Reputation: 2706
Assign your viewModel to a variable:
var viewModel = {...}
ko.applyBindings(viewModel, $("#html-id"));
To destroy the viewModel:
ko.cleanNode($("#html-id"));
delete viewModel;
$("#html-id").remove();
I haven't tried this yet, but it's what I'm planning to use in a new project that will have dynamically loaded/destroyed modules...
Upvotes: 2