2010-09-02 46 views
1

我在現有項目上使用Jquery UI自動完成。好了,表現狗緩慢,執行緩存Jquery UI自動完成Combobox

input.autocomplete("search", ""); 

我的解決辦法是緩存中的信息尤其是當,所以即使它的狗慢只發生一次。我想我錯過了一個非常簡單的Javascript錯誤,我真的很感謝一些幫助追逐它。

下面是代碼

input.autocomplete(
     { 
      delay: 0, 
      minLength: 0, 
      source: function (request, response) 
      { 
       if (request.term in cache) 
       { 
        response(cache[request.term]); 
        return; 
       } 
       // The source of the auto-complete is a function that returns all the select element's child option elements. 
       var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); 
       response(select.children("option").map(function() 
       { 
        var text = $(this).text(); 
        if (this.value && (!request.term || matcher.test(text))) 
        { 
         cache[request.term] = text; 
         return { label: text, value: text, option: this }; 
        } 
       })); 
      }, 

      select: function (event, ui) 
      { 
       // On the select event, trigger the "selected" event with the selected option. Also update the select element 
       // so it's selected option is the same. 
       ui.item.option.selected = true; 
       self._trigger("selected", event, 
       { 
        item: ui.item.option 
       }); 
      }, 

      change: function (event, ui) 
      { 
       // On the change event, reset to the last selection since it didn't match anything. 
       if (!ui.item) 
       { 
        $(this).val(select.children("option[selected]").text()); 
        return false; 
       } 
      } 
     }); 

     // Add a combo-box button on the right side of the input box. It is the same height as the adjacent input element. 
     var autocompleteButton = $("<button type='button' />"); 
     autocompleteButton.attr("tabIndex", -1) 
          .attr("title", "Show All Items") 
          .addClass("ComboboxButton") 
          .insertAfter(input) 
          .height(input.outerHeight()) 
          .append($("<span />")) 
     autocompleteButton.click(function() 
     { 
      // If the menu is already open, close it. 
      if (input.autocomplete("widget").is(":visible")) 
      { 
       input.autocomplete("close"); 
       return; 
      } 

      // Pass an empty string as value to search for -- this will display all results. 
      input.autocomplete("search", ""); 
      input.focus(); 
     }); 

幾乎所有的它與我的軟弱緩存的嘗試之外默認jQuery UI的組合框的示例代碼。它將返回下拉菜單中的每個字符。

例如,如果返回的解集是烏合之衆,和下一個被取得foobar「緩存數據」將如下所示 ˚F ö ö b 一個 ř 每個在其自己的行

我需要它是 烏茲別克人 foobar

這將是非常好的,如果這也適用於一個空字符串,因爲這是我最困難的調用。

感謝您的幫助

回答

0

如果您通過設置適當的HTTP頭來緩存webservice結果將會容易得多。這樣瀏覽器就會爲您處理所有令人討厭的緩存失效問題。

在C#中,你可以設置一個Expires頭一年之後的未來,像這樣:

Response.Cache.SetExpires(DateTime.Now.AddYears(1)); 

用於HTTP緩存最好的參考就是馬克諾丁漢Caching Tutorial for Web Authors and Webmasters。任何我沒有在這裏回答的東西都在那個文件中。

更多的C#特定資源是dotnetperls.com的ASP.Net Cache Examples and Overview

0

jQuery自動完成緩存默認情況下。你可能想要做的是做一些分頁或緩存服務器端來限制性能壓力。你在使用什麼服務器端技術?

+0

我們在這裏是一個.net商店,所以服務器端是C# – Aardvark 2010-09-02 22:56:49

+0

有幾種方法可以解決這個問題。我發現的最好方法是簡單地緩存整個結果集並在一段時間後過期。它的每一個後續查詢都會違背緩存。 – 2010-09-03 16:55:25