valueupdate在剔除中启用dirtyflag

时间:2018-11-08 17:37:26

标签: knockout.js

这是我的html

<textarea id="emp" data-bind="value: lastName, valueUpdate: 'afterkeydown', restrictnumberofwords : 50,  event: { keydown: test}" ></textarea>

这是删除代码

ko.bindingHandlers.restrictnumberofwords = {
update: function(element, valueAccessor, allBindingsAccessor, viewModel)
{

   element.value = element.value.substr(0, valueAccessor());
   allBindingsAccessor().value(element.value.substr(0, valueAccessor()));
}
};

function test(data, e) {

    //some logic which will restrict user to type specific number of characters
}

在我的实际代码中,我具有自动保存的功能,当我们在文本区域中键入内容时,该功能将使dirtyflag为true。但是我不想每次用户在textarea中键入都启用dirtyflag。仅当我们退出控件时才应启用自动保存。如何实现呢?

2 个答案:

答案 0 :(得分:0)

您可以使用textInput绑定进行实时更新,而使用value绑定进行模糊更新:

const liveInput = ko.observable("");
const slowInput = ko.observable("");
const charCount = ko.pureComputed(() => liveInput().length);

ko.applyBindings({ liveInput, slowInput, charCount });
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<input data-bind="textInput: liveInput, value: slowInput" placeholder="type to see two values sync">


<p>Live: <span data-bind="text: liveInput"></span></p>
<p>On blur: <span data-bind="text: slowInput"></span></p>
<p>Char. count: <span data-bind="text: charCount"></span></p>

答案 1 :(得分:0)

您的代码看起来不错,我认为您需要删除值更新。

<textarea id="emp" data-bind="value: lastName,  restrictnumberofwords : 50,  event: { keydown: test}" ></textarea>