javascript - How to remove bindings from node in knockout? - Stack Overflow

I've found that in theory ko.cleanNode() should remove bindings from node if called, but in this e

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

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

Share Improve this question asked Aug 13, 2013 at 0:41 skmasqskmasq 4,5217 gold badges44 silver badges77 bronze badges 1
  • Standard KO bindings don't track event listeners as such. cleanNode removes the "internal bindings". See stackoverflow./a/15069509/2246674 - I've found it best to simply play with KO as it wants to be played with. – user2246674 Commented Aug 13, 2013 at 0:50
Add a ment  | 

1 Answer 1

Reset to default 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/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'.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743753194a4501281.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信