提前键入JS“始终使用远程”

时间:2018-09-07 14:16:25

标签: typeahead.js twitter-typeahead bloodhound

我知道,仅当预取结果未超过设置的限制时才应使用远程。 我已将限制设置为1,并且预取具有多个条目。仍然使用远程获取数据,最终仅显示1个结果。

   initialise_quick_search: function () {

        var building_data = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('building_name'),
            queryTokenizer: Bloodhound.tokenizers.nonword,
            prefetch: {
                url: '/quick_search/quick_search_results?search_buildings=1',
                //cache: false
            },
            remote: {
                url: '/quick_search/quick_search_results?search_buildings=1&query=%QUERY',
                wildcard: '%QUERY',
                cache: false
            }
        });

        building_data.initialize();


        var $quick_search = $('#quick_search');

        $quick_search.typeahead({
                highlight: true,
                hint: false,
                minLength: 2,
            },
            {
                name: 'buildings',
                display: 'building_name',
                source: building_data,
                limit: 1,

                templates: {
                    header: 'Buildings',

                    suggestion: function (data) {

                        return '<p><a target="_blank" href=' + data.href + '>' + data.text + '</a></p>'
                    }
                }

            }
        );

        $quick_search.bind('typeahead:close', function (ev, suggestion) {
            $quick_search.val('');
            $quick_search.typeahead('close');
        });

        $quick_search.bind('typeahead:select', function (ev, suggestion) {
            window.open(suggestion.href, '_blank');
        });

    }

1 个答案:

答案 0 :(得分:0)

如果有人希望在预取中至少有一个条目时不触发远程,请在typeahead.bundle.js文件的以下代码段中更改足够

 var oParser = function() {
        "use strict";
        return function parse(o) {
            var defaults, sorter;
            defaults = {
                initialize: true,
                identify: _.stringify,
                datumTokenizer: null,
                queryTokenizer: null,
                matchAnyQueryToken: false,
                sufficient: 5,
                indexRemote: false,
                sorter: null,
                local: [],
                prefetch: null,
                remote: null