kickout.js更改hasfocus值不会更新绑定值 [英] knockout.js changing the hasfocus value does not update binding value
问题描述
我有 http://jsfiddle.net/ksCSn/1/
<input type="text" data-bind="
value: title,
hasfocus: edit,
onEnter: stopEdit" />
<p data-bind="text: title"></p>
JS
ko.bindingHandlers.onEnter = {
init: function(element, valueAccessor, _, viewModel) {
ko.utils.registerEventHandler(element, 'keydown', function(evt) {
if (evt.keyCode === 13)
valueAccessor().call(viewModel);
});
}
}
function ViewModel() {
this.title = ko.observable("default value");
this.edit = ko.observable(false);
this.stopEdit = function() {
this.edit(false);
// If the edit update is in a timeout, then it works
// var edit = this.edit;
// setTimeout(function() { edit(false); }, 0);
};
}
ko.applyBindings(new ViewModel());
在输入字段中进行编辑的同时按Enter键怎么办,该值不更新?
How come when the Enter key is pressed while editing in the input field, the value does not update?
如果我更改了编辑更新,以使其作为超时排队,那么它将起作用.为什么会这样?
And if I change the edit update so that it is queued up as a timeout, then it works. Why is that?
推荐答案
这是由于淘汰赛中的错误"造成的(请参阅 https://github.com/SteveSanderson/knockout/issues/321 ),这会导致所有绑定一起更新.更改edit
属性时,它将更新hasfocus
绑定以模糊该字段,并且由于该错误,也将更新value
绑定.由于绑定是按照列出的顺序运行的,因此value
绑定将首先更新,这将在视图模型中使用title
的值覆盖该字段.
This is because of a "bug" in Knockout (see https://github.com/SteveSanderson/knockout/issues/321) that causes all bindings to update together. When you change your edit
property, it updates the hasfocus
binding to blur the field, and, because of the bug, updates the value
binding too. Because bindings are run in the order listed, the value
binding gets updated first, which overwrites the field with the value of title
in the view model.
解决此问题的一个简单更改是重新排序绑定,以便首先运行hasfocus
: http://jsfiddle.net/mbest/ksCSn/8/
A simple change that fixes this is to re-order the bindings so that hasfocus
will run first: http://jsfiddle.net/mbest/ksCSn/8/
这篇关于kickout.js更改hasfocus值不会更新绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!