6

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
  • 4,470
  • 6
  • 42
  • 77
  • Standard KO bindings don't track event listeners as such. `cleanNode` removes the "internal bindings". See http://stackoverflow.com/a/15069509/2246674 - I've found it best to simply play with KO as it wants to be played with. – user2246674 Aug 13 '13 at 00:50

1 Answers1

8

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.

http://jsfiddle.net/BrsmC/2/

Take out line 21 of the updated fiddle.

ko.cleanNode($("#testing")[0]);

You should see when you run it, the name is now 'imnotbinding'.

Johnny Tops
  • 610
  • 1
  • 6
  • 10
  • 5
    Does `cleanNode` work if passed an element that contains multiple, nested bindings, or does it require the node and binding itself? – raffian Feb 03 '14 at 03:06