【发布时间】:2016-09-07 11:18:22
【问题描述】:
我正在尝试使用 Typeahead 和 Bloodhound 框架从远程 API 调用中搜索 api 路径的响应。
通过示例 url api.example.com/getEndpoints 我会收到一个包含所有端点的对象。 我希望预先输入来过滤这些端点。据我了解,您想在使用遥控器时指定一个查询,我显然不能,因为我只是在一个请求中接收所有端点。
有什么方法或建议可以解决这个问题吗?
我的代码看起来像
// Instantiate the Bloodhound suggestion engine
var endpoints = new Bloodhound({
datumTokenizer: function (datum) {
console.log('datumTokenizer, datum: ', datum, ', datum.value: ', datum.value);
return Bloodhound.tokenizers.whitespace(datum.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'https://api.example.com/getEndpoints',
filter: function (endpoints) {
console.log(endpoints);
// Map the remote source JSON array to a JavaScript object array
return Object.keys(endpoints).map(function (endpointPath) {
console.log(endpointPath);
return {
value: endpointPath
};
});
}
},
limit: 10
});
// Initialize the Bloodhound suggestion engine
endpoints.initialize();
// Instantiate the Typeahead UI
$('#inputPath').typeahead(
{
hint: true,
highlight: true,
minLength: 1
}, {
displayKey: 'value',
source: endpoints.ttAdapter()
}
);
【问题讨论】:
-
我还需要什么解决方案?
-
不 - 我相信我从未找到解决该问题的方法,因此决定使用另一个框架来解决我的问题。对不起
标签: javascript typeahead.js bloodhound