Thomas
Thomas

Reputation: 4719

Knockoutjs cleanup on DOM removal

I will be using knockoutjs in a single page application and I am concerned whether the observables remain in memory even if any DOM elements that the bindings were applied to, are removed. What I think is that I will need to handle this in my application, by calling

ko.cleanNode(DOMElement)

on each DOM element that used observables, before removing them from the document. I just need someone to confirm that this is the case

Thanks

Upvotes: 3

Views: 135

Answers (1)

DoctorMick
DoctorMick

Reputation: 6793

CleanNode doesn't remove the observables, it just unbinds them from the UI elements. You'd also need to remove any references to your view models by setting them to null in order for them to be garbage collected, something like:

var myVM = new myViewModel();
ko.applyBindings(myVM, DOMElement);

//All your other stuff

ko.cleanNode(DOMElement);
myVM = null;

Upvotes: 3

Related Questions