Typeahead 0.10防止缓存

Vic*_*anu 6 typeahead typeahead.js twitter-typeahead

我使用twitter的typeahead 0.10和远程url从服务器检索JSON结果.

我想阻止客户端缓存,以便始终在服务器上进行搜索.我怎样才能做到这一点?

请看下面我的代码:

 // instantiate the bloodhound suggestion engine
    var dataSource = new Bloodhound({
        datumTokenizer: function (d) {
            return Bloodhound.tokenizers.whitespace(d.value);
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: "../" + autocompleteInfo.ControllerName + "/" + autocompleteInfo.MethodName + "?term=%QUERY&ts=" + (new Date().getTime()),
            filter: function (res) {
                var data = [];
                data = $.map(res, function (item) {
                    return { label: item.Name, id: item.Id, autocompleteInfo: autocompleteInfo, cssClass: item.Class };
                });

                return data;
            }
        },
        limit: 15,
        name: 'typeaheadSourceCache',
        ttl: 0,
        ajax: {
            cache: false
        }
    });

    dataSource.initialize();

    $("#" + autocompleteInfo.AutocompleteId).typeahead({
        minLength: 3,
        highlight: true,
        autoselect: true
    },
        { 
            displayKey: 'label',
            source: dataSource.ttAdapter(),
            templates: {
                suggestion: Handlebars.compile(
                '<div class="searchItem {{cssClass}}">{{label}}</div>'
                )
            }
        });
Run Code Online (Sandbox Code Playgroud)

小智 28

只需cacheremote对象添加字段:

remote: { 'cache': false ... }

  • 应该选择这个作为答案.谢谢理查德. (3认同)

Phi*_*hil 5

查看版本10.0.2。现在有一种方法可以通过Bloodhound.js(与Typeahead.js结合使用)清除缓存:

engine.clearRemoteCache();

这是来自Twitter typeahead的文档:https : //github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#bloodhoundclearremotecache