gpt4 book ai didi

javascript - twitter typeahead.js process() 使用未定义填充列表

转载 作者:行者123 更新时间:2023-12-01 05:50:57 28 4
gpt4 key购买 nike

我似乎和Twitter Typeahead Ajax results undefined有同样的问题但这还没有解决,所以我再次询问,希望我能提供一些缺失的细节。

我正在使用独立的 typeahead 0.10.2 和 bootstrap 2.3.1。下划线是1.6.0。包含的库的完整列表是:

<script src="/static/portal/js/jquery-1.9.1.min.js"></script>
<script src="/static/portal/js/jquery-ui-1.10.2.custom.min.js"></script>
<script src="/static/portal/js/bootstrap.min.js"></script>
<script src="/static/portal/js/typeahead.bundle.min.js"></script>
<script src="/static/portal/js/hogan-2.0.0.min.js"></script>
<script src="/static/portal/js/underscore-min.js"></script>

在我的第一次尝试中,我在 http://fusiongrokker.com/post/heavily-customizing-a-bootstrap-typeahead 上建模了我的脚本。 ,一切似乎都正常,直到调用 process()。它不是用我的结果填充自动建议下拉列表,而是只包含“未定义”值(尽管元素数量正确)。

$("#create_track").click(function() {
var names_id_map = {};
$('#ul_results').addClass('hide');
$('#create_track_form').removeClass('hide');
// use debounce from underscore.js to throttle requests to server
var suggestArtists = _.debounce(function(query, process) {
var artist_names = [];
$.get(ARTIST_NAMES_URL, { q: query }, function (data) {
// data = {"results": [{"id": "artist_123", "name": "xyz"}, ...]}
$.each(data, function (key, val) {
if (key == "results") {
$.each(val, function (i, item) {
artist_display = item.name + ' - ' + item.id;
artist_names.push(artist_display);
names_id_map[artist_display] = item.id;
});
}
});
process(artist_names);
});
}, 300); // rate limit requests to server

$("#artist_name").typeahead(null, {
name: 'artist-names',
minLength: 1,
source: function (query, process) {
suggestArtists(query, process);
},
updater: function (item) {
console.dir('updater: ' + item); // never output
$("#artist_id").val(names_id_map[item]);
// return value which should be selected in drop-down
// return item;
},
});

return false;
});

该特定教程可能是为旧的 bo​​otstrap-bundled 版本的 typeahead 创建的。使用较新的独立版本的 typeahead 的文档,我想出了这个,其行为相同,从而产生了一个包含正确数量的未定义的菜单:

$("#create_track").click(function() {
var names_id_map = {};
$('#ul_results').addClass('hide');
$('#create_track_form').removeClass('hide');

var artistNames = new Bloodhound({
name: 'artist-names',
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: ARTIST_NAMES_URL + '?q=%QUERY',
//rateLimitBy: debounce, // debounce is not defined?
rateLimitWait: 300,
filter: function(data) {
var artist_names = [];
// data = {"results": [{"id": "artist_123", "name": "xyz"}, ...]}
$.each(data, function (key, val) {
if (key == "results") {
$.each(val, function (i, item) {
artist_display = item.name + ' - ' + item.id;
artist_names.push(artist_display);
names_id_map[artist_display] = item.id;
});
}
});
return artist_names;
},
}
});
artistNames
.initialize()
.done(function() { console.log('artistNames init success'); });

$("#artist_name").typeahead(null, {
minLength: 1,
source: artistNames.ttAdapter(),
});

return false;
});

如果我用这样的本地数据填充它:

$("#create_track").click(function() {
var names_id_map = {};
$('#ul_results').addClass('hide');
$('#create_track_form').removeClass('hide');

var artistNames = new Bloodhound({
name: 'artist-names',
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: ["Abc", "Def", "Ghi", "Jkl"],
});
artistNames.initialize()
.done(function() { console.log('artistNames init success'); });

$("#artist_name").typeahead(null, {
minLength: 1,
source: artistNames.ttAdapter(),
});

return false;
})

例如,我可以输入“a”或“d”,然后得到一个“未定义”建议。输入“b”、“c”或“z”不会给出任何建议。

过去一天,我在 Google & SO 上阅读了几乎所有我能找到的有关 typeahead 的内容。我确信我的 JSON 已正确解码,并且在所有情况下我的艺术家名称都是 native JS 字符串数组。

最佳答案

重新开始 https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#api 处的示例(并由另一个重复的 Why does bloodhound.get() return undefined? 提示),我意识到我的本地值数组实际上需要是与 datumTokenizer 和 typeahead 的 displayKey 合作的哈希/字典。我现在有一个简单的示例,用于单前导字母建议:

var artistNames = new Bloodhound({ 
name: 'artist-names',
datumTokenizer: function (d) { return d.d; },
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [{"d": "Abc"}, {"d": "Aaa"}, {"d": "Aab"}, {"d": "Def"}, {"d": "Ghi"}, {"d": "Jkl"}, {"d": "John Coltrane"}],
});
artistNames.initialize().done(function() { alert('setup ok'); });

$("#artist_name").typeahead(null, {
minLength: 1,
displayKey: "d",
source: artistNames.ttAdapter(),
});

我的初始语法来 self 通过谷歌找到的一些教程,这些教程可能使用旧的 Bootstrap 版本的库(例如,来 self 链接到这篇文章@顶部的教程:“然后一个简单的名称数组 (['John Smith','Jane Smith',...]) 被传递给 process() 回调。")。

关于javascript - twitter typeahead.js process() 使用未定义填充列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22506237/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com