2014-02-18 64 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; 
}