没有查询的Typeahead远程

时间:2016-09-07 11:18:23

标签: javascript typeahead.js bloodhound

我正在尝试使用Typeahead和bloodhound框架从远程API调用中搜索api路径的响应。

通过示例url api.example.com/getEndpoints我会收到一个包含所有端点的对象。 我希望typeahead过滤这些端点。据我所知,你想在使用遥控器时指定一个查询,我显然不能,因为我只是在一个请求中收到所有端点。

有没有办法或建议来解决这个问题?

我的代码看起来像

    // 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()
    }
);

1 个答案:

答案 0 :(得分:0)

我认为你想要prefetch而不是remote

查看https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#prefetch

相关问题