How to remove bindings from node in knockout?
13,926
Knockout is removing the knockout related bindings from the node, but when it does so, it does not reset the node to empty values. It just stops updating the node automatically from the viewmodel, vm.
Take out line 21 of the updated fiddle.
ko.cleanNode($("#testing")[0]);
You should see when you run it, the name is now 'imnotbinding'.
Related videos on Youtube
Author by
skmasq
Updated on September 27, 2022Comments
-
skmasq over 1 year
I've found that in theory
ko.cleanNode()
should remove bindings from node if called, but in this example it doesn't seem to work.Javascript:
// View model var vm = { name: ko.observable("John") } // Node to be added var node = $("<div/>",{ id: "testing", 'data-bind' : "text: name()" }); // First addition to body $("body").append(node); // Apply bindings ko.applyBindings(vm,$("#testing")[0]); // Remove ko.cleanNode($("#testing")[0]); $("#testing").remove(); $("body").append(node);
Result: You can see in jsFiddle , that node still has attached binding (event listener).
-
skmasq over 10 yearsThanks, didn't think of that per say.
-
raffian over 10 yearsDoes
cleanNode
work if passed an element that contains multiple, nested bindings, or does it require the node and binding itself?