Typehead.js 奇怪的远程行为 - 有时不显示结果

Typehead.js strange behaviour remotely - sometimes not showing result

我使用最新的 typehead (typeahead.js 0.11.1) 作为 typeheadbundle 时发生了很多奇怪的事情

首先,我使用 typeahead.bundle.min.js v0.11.1 远程使用 2 个数据集。问题是服务器响应正确,但 typehead 根本不关心,并说找不到任何东西。

奇怪的是,有时打字头会正确显示结果,有时只有一个数据集,有时什么都不显示。但是服务器给出结果!

更新

我发现 typehead 在最新的稳定版本中有一个错误。这里是固定的https://github.com/corejavascript/typeahead.js

typehead 在最新的稳定版本中有错误 :)

我不相信。

直到发现一些好人制作了一个叉子并修复了它 https://github.com/corejavascript/typeahead.js

我虽然如果这是由 Twitter 维护的 - 而且我使用的是最新的稳定版本 - 可能我做错了什么,但即使是大牌也失败了:(

我明白了。

刚开始使用typehead的时候,我也遇到了一些错误,但是后来我发现了,那是我的错。

使用 typehead.js. So, I have created a complete example of using typehead.js 使用本地数据和使用服务器数据 (AJAX) 需要一些配置。

您可以查看this Github Repo


重新

为了您的理解,我在这里做一个小演示(没有CSS)-

var substringMatcher = function(strs)
{
 return function findMatches(q, cb)
 {
  var matches, substrRegex;

  // an array that will be populated with substring matches
  matches = [];

  // regex used to determine if a string contains the substring `q`
  substrRegex = new RegExp(q, 'i');

  // iterate through the pool of strings and for any string that
  // contains the substring `q`, add it to the `matches` array
  $.each(strs, function(i, str)
  {
   if (substrRegex.test(str))
   {
    // the typeahead jQuery plugin expects suggestions to a
    // JavaScript object, refer to typeahead docs for more info
    matches.push({ value: str });
   }
  });

  cb(matches);
 };
};

var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

$('#typehead_example').typeahead(
      {
       hint:  true,
       highlight: true,
       minLength: 1
      },
      {
       name:  'states',
       displayKey: 'value',
       source:  substringMatcher(states)
      }
     );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/corejs-typeahead/1.0.1/typeahead.bundle.min.js" type="text/javascript" charset="utf-8"></script>

<input id="typehead_example" class="typeahead" type="text" placeholder="States of USA">

完整示例在 this Github Repo 中给出。