【问题标题】:Typeahead 0.10 prevent cachingTypeahead 0.10 防止缓存
【发布时间】:2014-02-18 14:12:10
【问题描述】:

我使用 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>'
                )
            }
        });

【问题讨论】:

标签: typeahead typeahead.js twitter-typeahead


【解决方案1】:

只需将cache 字段添加到remote 对象:

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

【讨论】:

  • 这正是我所需要的。我有多个猎犬引擎,与多个文本输入相关联,每个引擎都按角色提供不同的用户列表(这是一个拗口)。从三个文本输入中的一个中选择一个用户后,其他输入将显示第一个输入提供的用户列表。就我的目的而言,这实际上在逻辑上是不正确的。无论如何,谢谢理查德。
  • 应该选择这个作为答案。谢谢理查德。
  • 这绝对应该是选择的答案。为什么在github.com/twitter/typeahead.js/blob/master/doc/…的文档中的“远程”下没有提到这个选项@
  • 这绝对有效!请记住,要刷新结果(如果您向数据源添加了更多结果),您必须更改当前预输入搜索的至少一个字母。非常感谢!
【解决方案2】:

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

engine.clearRemoteCache();

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

【讨论】:

    【解决方案3】:

    尝试使用 typeahead destroy utils,我认为你的情况是:

    $("#" + autocompleteInfo.AutocompleteId).typeahead('destroy');
    

    你重新初始化 $("#" + autocompleteInfo.AutocompleteId)

    【讨论】:

    • 谢谢。我不想再次销毁并重新创建预输入控件(或至少是预输入使用的输入元素),原因之一是我没有适当的事件何时执行此销毁/创建操作。我希望为每个搜索的关键字(如果之前搜索过它)向服务器发出请求。
    【解决方案4】:

    为了解决我遇到的 IE 问题:

    remote: {
    url: '/myurl?par=%QUERY',
    wildcard: '%QUERY',
    prepare: function (q, o) {
            o.url = o.url.replace('%QUERY', encodeURIComponent(q));
            o.cache = false;
            return o;
        }
    }
    
    prefetch: {
        url: '/myurl2',
        ttl: 300000, //5min
        thumbprint: userName,
        prepare: function(o) {
            o.cache = false;
            return o;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 2020-05-27
      • 2015-04-10
      • 1970-01-01
      相关资源
      最近更新 更多