javascript - Twitter Typeahead 与预取 JSON

标签 javascript jquery ruby-on-rails json bootstrap-typeahead

所以我尝试使用 Twitter 的 Typeahead 自动完成库。我预取一些我拥有的 JSON 数据,然后将我需要的信息放入一个数组中。我的代码如下:

var test = new Bloodhound({
    datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
    url: 'http://localhost:3000/suggestions',
      filter: function(list) {
        nameArray = [];
        for( var i = 0; i < list.length; ++i){
          nameArray.push(list[i].name);
        }
        window.alert(nameArray)
        return nameArray;
      }
    }
});

test.initialize();

$('.example-test .typeahead').typeahead(null, {
    name: 'test',
    source: test.ttAdapter()
});

我使用警报来确保数组是我想要的,它只是一个字符串数组。

上面的代码似乎没有产生任何结果。我没有自动完成功能。数组已正确生成,所以我不确定问题是什么。我已经使用 Twitter 提供的示例测试了我的 HTML 和 CSS,它们工作完美。非常感谢任何帮助。

最佳答案

竞争条件 - 您只需在 ajax 请求成功后应用 typeahead 调用。

将最后一个 block 放入成功函数中,你应该会很好。

关于javascript - Twitter Typeahead 与预取 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22080609/

相关文章:

jquery - 向 JQueryStatic 接口(interface)添加属性

javascript - 如何在 javascript chrome 书签代码中使用 jQuery?

mysql - ruby on rails- 拒绝用户访问 'root' @'localhost'

sql - 将数组传递给 Rails where 方法

javascript - Heroku 没有运行 gulp 吗?

javascript - 展开变量内的标签

javascript - jQuery: "$(this).next().next()"有效,但 "$(this).next(' .div')"无效

javascript - 选择第二个日期。推特 Bootstrap

jquery - 如何在鼠标悬停或悬停时连续重复触发?

ruby-on-rails - Rails 博客 : Edit is Creating a Duplicate Record