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.

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'.

Share:
13,926

Related videos on Youtube

skmasq
Author by

skmasq

Updated on September 27, 2022

Comments

  • skmasq
    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
    skmasq over 10 years
    Thanks, didn't think of that per say.
  • raffian
    raffian over 10 years
    Does cleanNode work if passed an element that contains multiple, nested bindings, or does it require the node and binding itself?