【问题标题】:Typeahead remote without query无需查询的预输入遥控器
【发布时间】: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


【解决方案1】:

我认为你想要prefetch 而不是remote

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

【讨论】:

  • 我也这么认为 - 试过了,但它不会工作.... :-) 你有任何代码示例来说明如何正确地做到这一点,也许?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
  • 2017-11-07
  • 1970-01-01
相关资源
最近更新 更多