[英]Knockout custom binding update not working
I am trying to add a custom binder for hasFocus to work like this 我正在尝试为hasFocus添加自定义活页夹以使其工作
ko.bindingHandlers.hasfocus = {
//update the control when the view model changes
update: function (element, valueAccessor) {
ko.unwrap(valueAccessor());
alert('update');
setTimeout(function () {
alert(3);
if (value
&& element.offsetWidth && element.offsetHeight
&& document.activeElement && document.activeElement != element) {
element.focus();
ko.utils.triggerEvent(element, "focusin"); // For IE, which doesn't reliably fire "focus" or "blur" events synchronously
}
});
}
}; };
But it never comes into this function ever. 但是它永远不会进入此功能。 I am following this example
我正在关注这个例子
http://jsfiddle.net/mbest/tAGmp/ http://jsfiddle.net/mbest/tAGmp/
The thing is, my input field is not visible in the start. 关键是,我的输入字段一开始就不可见。 If I click some place then it gets visible.
如果我单击某个地方,那么它就会变得可见。 The input field looks like this
输入字段如下所示
<input type="text" data-bind="hasFocus: true" />
I was trying to make it work with hard code value true. 我试图使它与硬代码值true一起工作。 If it works then I will change it to some observable.
如果可以,那么我将其更改为可观察到的。 Any thoughts?
有什么想法吗?
I have update the fiddle here. 我在这里更新了小提琴。 As you can see in your console, it is fired initially and when a value is typed in.
正如您在控制台中看到的那样,它会在初始输入值时触发。
http://jsfiddle.net/tAGmp/11/ http://jsfiddle.net/tAGmp/11/
ko.bindingHandlers.hasfocus = {
update: function(element, valueAccessor) {
console.log('update');
var value = ko.utils.unwrapObservable(valueAccessor());
setTimeout(function() {
if (value
&& element.offsetWidth && element.offsetHeight
&& document.activeElement && document.activeElement != element)
{
element.focus();
ko.utils.triggerEvent(element, "focusin"); // For IE, which doesn't reliably fire "focus" or "blur" events synchronously
}
},0);
}
};
Hopes this helps. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.