简体   繁体   English

ko.cleanNode删除jQuery .on(“ focusout”)事件

[英]ko.cleanNode removes the jQuery .on(“focusout”) event

I am working with Knockout JS application. 我正在使用Knockout JS应用程序。 Here i have a problem when i use ko.cleanNode(node) , it removes focusout client event. 在这里,当我使用ko.cleanNode(node)时,我遇到了一个问题,它删除了focusout客户端事件。 ie, before calling cleanNode method focusOut works properly on text box. 即,在调用cleanNode方法之前,focusOut在文本框中可以正常工作。 After that focusOut event does not triggered. 之后,不会触发focusOut事件。 I have checked many of the stack overflow questions, I have only found the cause of this removal, not any alternative solution. 我检查了许多堆栈溢出问题,但仅找到了导致此溢出的原因,没有任何其他解决方案。 Here is my Code. 这是我的代码。

 $("#btn").click(function() { viewModel = { nValue: ko.observable(10) } ko.cleanNode(document.getElementById("txt")); ko.applyBindings(viewModel, document.getElementById("txt")); }); ko.applyBindings({ nValue: ko.observable(100) }); $("#txt").on("focusout", function() { alert("focusOut"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <input type="text" id="txt" data-bind="value: nValue" /> <input type="button" id="btn" value="cleannode" /> 

From the Knockout documentation on disposal logic : 从Knockout有关处理逻辑的文档中:

When removing an element, Knockout runs logic to clean up any data associated with the element. 删除元素时,淘汰赛会运行逻辑以清理与该元素关联的所有数据。 As part of this logic, Knockout calls jQuery's cleanData method if jQuery is loaded in your page . 作为此逻辑的一部分, 如果将jQuery加载到页面中 ,则Knockout会调用jQuery的cleanData方法 In advanced scenarios, you may want to prevent or customize how this data is removed in your application. 在高级方案中,您可能希望阻止或自定义如何在应用程序中删除此数据。 Knockout exposes a function, ko.utils.domNodeDisposal.cleanExternalData(node) , that can be overridden to support custom logic. 淘汰赛提供了一个函数ko.utils.domNodeDisposal.cleanExternalData(node) ,可以重写该函数以支持自定义逻辑。 For example, to prevent cleanData from being called, an empty function could be used to replace the standard cleanExternalData implementation 例如,为了防止调用cleanData,可以使用空函数代替标准的cleanExternalData实现。

So, override the cleanExternalData as mentioned in the documentation: 因此,按照文档中的说明覆盖cleanExternalData

 ko.utils.domNodeDisposal.cleanExternalData = function () { // Do nothing. Now any jQuery data associated with elements will // not be cleaned up when the elements are removed from the DOM. }; $("#btn").click(function() { viewModel = { nValue: ko.observable(10) } ko.cleanNode(document.getElementById("txt")); ko.applyBindings(viewModel, document.getElementById("txt")); }); ko.applyBindings({ nValue: ko.observable(100) }); $("#txt").on("focusout", function() { alert("focusOut"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <input type="text" id="txt" data-bind="value: nValue" /> <input type="button" id="btn" value="cleannode" /> 


Looking at your code, if your intention is just updating the viewmodel's observable upon a click and adding a focusOut event handler, you don't need to cleanNode or applyBindings() or use jquery at all. 查看您的代码,如果您的意图只是单击一下更新viewmodel的observable并添加focusOut事件处理程序,则根本不需要cleanNodeapplyBindings()或完全使用jquery。 You can use knokcout's click and event binding like this: 您可以像这样使用knokcout的clickevent绑定:

 var viewModel = function() { var self = this; self.nValue = ko.observable(100); self.increment = function() { self.nValue(self.nValue() + 1); } self.focusOut = function() { alert("Focus Out"); } }; ko.applyBindings(new viewModel()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <input type="text" id="txt" data-bind="value: nValue, event: { focusout: focusOut }" /> <input type="button" id="btn" value="increment" data-bind="click: increment" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM