最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论