Javascript 使用预取JSON的TwitterTypeahead
所以我尝试使用Twitter的Typeahead自动完成库。我预取一些我拥有的JSON数据,然后将我需要的信息放入一个数组中。我的代码如下所示:Javascript 使用预取JSON的TwitterTypeahead,javascript,jquery,ruby-on-rails,json,bootstrap-typeahead,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
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()
});
var测试=新猎犬({
datumTokenizer:function(d){return Bloodhound.tokenizers.whitespace(d.name);},
queryTokenizer:猎犬,标记,空白,
限额:10,
预取:{
网址:'http://localhost:3000/suggestions',
过滤器:函数(列表){
nameArray=[];
对于(变量i=0;i
我使用警报来确保数组是我想要的,它只是一个字符串数组
但是上面的代码似乎没有产生任何结果。我没有自动完成功能。数组生成正确,所以我不确定问题出在哪里。我已经使用Twitter提供的示例测试了我的HTML和CSS,它们工作得完美无缺。非常感谢您的帮助。竞争条件-您只需在ajax请求成功后应用typeahead调用
将最后一块放在成功函数中,您应该会做得很好。您的答案能否更明确一点?我试图将“$('.example test.typeahead').typeahead(null,{name:'test',source:test.ttAdapter()}”);”放在函数中,但这似乎并没有解决问题。抱歉,有点javascript新手。谢谢您的帮助。您只需在预回迁完成后调用该块(生成typeahead)。我不熟悉您的模型,我不确定在哪里告诉您调用所述函数。