2010-09-02 47 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