推特提前输入,KnockoutJS 未触发 onchange/update 在 Chrome/Explorer 中可观察到

Twitter typeahead, KnockoutJS not firing onchange/update observable in Chrome/Explorer

我将 twitter typeahead 与 knockout 一起使用,我注意到在从 typeaheads 建议中进行选择时,输入绑定的可观察对象未在 chrome/explorer 中更新。为了更新它,您必须在选择其中一个建议后进行击键(如 space)。 Enter/unfocusing 输入不够(但在 firefox 中有效)。

请使用以下 fiddle 和 chrome/explorer 与 firefox 进行测试:

http://jsfiddle.net/s415L158/5/

    var viewModel = {
    city:ko.observable()   
}

var cities = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: $.map(["Stockholm","Oslo","Copenhagen"], function(city) { return { value: city }; })
});
cities.initialize();

$('#typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'cities',
displayKey: 'value',
source: cities.ttAdapter()
}); 

ko.applyBindings(viewModel);

有什么解决办法吗?

更新:

重现:

  1. 开始在输入
  2. 中输入'S'
  3. Select 'Stockholm' 来自建议
  4. 不聚焦输入
  5. Observable 未更新

我通过添加一个自定义事件来完成这项工作,该事件会在选择建议时更新变量,如下所示:

$('#typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'cities',
displayKey: 'value',
source: cities.ttAdapter()
}).on('typeahead:selected', function(event, selection){
viewModel.city(selection.value);
});