javascript 如何在淘汰赛中从节点中删除绑定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18198931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove bindings from node in knockout?
提问by skmasq
I've found that in theory ko.cleanNode()
should remove bindings from node if called, but in this exampleit doesn't seem to work.
我发现理论上ko.cleanNode()
应该从 node 中删除绑定,如果被调用,但在这个例子中它似乎不起作用。
Javascript:
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).
结果:您可以在jsFiddle中看到,该节点仍然具有附加绑定(事件侦听器)。
回答by Johnny Tops
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.
Knockout 正在从节点中删除与敲除相关的绑定,但是当它这样做时,它不会将节点重置为空值。它只是停止从视图模型 vm 自动更新节点。
Take out line 21 of the updated fiddle.
取出更新后的小提琴的第 21 行。
ko.cleanNode($("#testing")[0]);
You should see when you run it, the name is now 'imnotbinding'.
您应该在运行它时看到,名称现在是“imnotbinding”。