使用Bloodhound时如何将Ajax全局事件设置为false

时间:2018-07-09 19:49:42

标签: ajax globalevent

我正在使用typeahead进行自动填充。以下代码可以正常工作。

var employees = new Bloodhound({
    datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.name); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    addOnBlur: true,

    remote: {
        url: $url + '?name=%QUERY',
        global: false,
        wildcard: '%QUERY',
        filter: function (response) {
            return $.map(response.results, function (employee) {

                return {
                    mark: employee.MARK,
                    actitle: employee.ACCOUNTTITLE, 
                    code: employee.CODE
                }
            });
        }
    }

});

employees.initialize();

$($classname).typeahead({ highlight: true, minLength: 1, limit: 5 }, {
    name: 'employees', displayKey: 'mark', source: employees.ttAdapter(), global: false
})
    .on("typeahead:selected", function (obj, company1) {
        //debugger;
    $($retField).val(company1.actitle);
    $($cField).val(company1.code);
})
    .on('focusout', function (obj, company) {
        //debugger;
        $($classname).trigger("typeahead:first-child");
 });

}

我想将ajax全局事件设置为false。 请帮助我解决问题

1 个答案:

答案 0 :(得分:0)

迟到了聚会,但我想通了。使用transport属性:

var employees = new Bloodhound({
    datumTokenizer: function(d) {
        return Bloodhound.tokenizers.whitespace(d.name);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    addOnBlur: true,
    remote: {
        url: $url + '?name=%QUERY',
        global: false,
        wildcard: '%QUERY',
        transport: function(opts, onSuccess, onError) {
            var url = opts.url.split("#")[0];
            var query = opts.url.split("#")[1];
            $.ajax({
                url: url,
                data: "search=" + query,
                type: "POST",
                success: onSuccess,
                error: onError,
                global: false
            })
        }
        filter: function(response) {
            return $.map(response.results, function(employee) {
                return {
                    mark: employee.MARK,
                    actitle: employee.ACCOUNTTITLE,
                    code: employee.CODE
                }
            });
        }
    }
});
employees.initialize();

基于其他answer堆栈溢出: