2014-02-18 102 views
5

我使用twitter的typeahead 0.10与远程URL从服务器检索JSON结果。Typeahead 0.10防止缓存

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

请参阅下面我的代码:

// 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>' 
       ) 
      } 
     }); 
+0

知道其他人在twitte可能会感兴趣r typeahead社区也有同样的问题,正在研究解决方案。例如,请参阅:https://github.com/twitter/typeahead.js/pull/703 – Phil

+0

另请参阅https://github.com/twitter/typeahead.js/issues/564 –

回答

0

尝试使用预输入摧毁utils的,我觉得你的情况是:

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

的你reinizialize $( “#” + autocompleteInfo.AutocompleteId )

+0

谢谢。我不想销毁并重新创建typeahead控件(或者至少typeahead使用的输入元素),原因之一是我没有适当的事件来执行此销毁/创建操作。我想为搜索到的每个关键字,事件(如果它之前被搜索过)向服务器发出请求。 –

25

只需添加cacheremote对象:

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

+0

这正是我所需要的。我有多个猎犬引擎,绑定到多个文本输入,每个角色提供不同的用户列表(这是一口)。从三个文本输入之一中选择一个用户后,其他输入将显示第一个输入提供的用户列表。对我而言,这在逻辑上实际上是不正确的。无论如何,谢谢理查德。 – Dan

+2

应该选择这个作为答案。感谢Richard。 – DerProgrammer

+0

这绝对应该是选定的答案。为什么地球上不是这个选项在https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#remote –

0

要解决,我来到了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; 
}