以编程方式更新webshim日期
我正在使用带有敲除绑定的html5 < input type =date>
。我正在将webshim用于不支持html5日期的浏览器。
I'm using an html5 <input type="date">
with a knockout binding. I'm using webshim for browsers that don't support html5 dates.
本机html5浏览器工作正常,更改日期更改模型,更改模型以编程方式更改日期输入中显示的日期。
The native html5 browsers are working perfectly, changing the date changes the model, changing the model programmatically changes the date shown in the date input.
更改不支持html5的浏览器上的日期(例如IE8)可以正常工作并更新淘汰模型,但相反的是不对。对模型的更改不会传播到webshim生成的日期选择器,而只会传播到webshim使用的隐藏输入。
Changing the date on a browser that does not support html5 (e.g. IE8) works correctly and updates the knockout model, but the reverse is not true. Changes to the model are not propagated to the webshim-generated date picker, only to the hidden input that webshim uses.
是否存在我可以调用的webshim提供的方法或事件或者火来告诉它查看数据并在更改后更新UI?我怎么能写一个敲除绑定来调用它?
Is there a webshim provided method or event I can call or fire to tell it to look at the data and update the UI after a change? How could I write a knockout binding to call this?
事实证明,webshim要求你使用jQuery()。val( )更新日期,而不是直接使用DOM。我能够通过扩展正常值绑定来编写一个敲除绑定:
It turns out webshim requires that you use jQuery().val() to update the date, rather than using the DOM directly. I was able to write a knockout binding that did this by extending the normal value binding:
ko.bindingHandlers.date = $.extend({}, ko.bindingHandlers.value);
ko.bindingHandlers.date.update = function(element, valueAccessor) {
// Set value using jQuery val method as this is caught internally by webshim
$(element).val(valueAccessor()());
};
然后我可以使用:
<input type="date" data-bind="'date': date">
正如所料。