2015-02-05 35 views
1

我在我的web應用程序中使用typeahead.js 0.10.5。出於一些奇怪的原因,通過遠程工作獲取建議,而預取損壞。這裏有一些非明顯和奇怪的事情。根據開發控制檯和Chrome的網絡監視器,它甚至沒有對頁面加載進行查詢。當然,當我開始輸入時,它會進行查詢。Typeahead.js遠程工作,但預取不

這真的讓我難住 - 我在這裏做錯了什麼?

// Instantiate the Bloodhound suggestion engine 
    var tags = new Bloodhound({ 
     datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), 
     queryTokenizer: Bloodhound.tokenizers.whitespace, 
     remote: { 
     url: '/tags/tags/search.json?q=%QUERY', 
     filter: function (taglist) { 
      // Map the remote source JSON array to a JavaScript object array 
      return $.map(taglist, function (tag) { 
       console.log(tag); 
       return { 
        value: tag.tag 
       }; 
      }); 
     } 
     }, 
     prefetch: { 
     url: '/tags/tags/search.json?q=', 
     filter: function (taglist) { 
      // Map the remote source JSON array to a JavaScript object array 
      return $.map(taglist, function (tag) { 
       console.log(tag); 
       return { 
        value: tag.tag 
       }; 
      }); 
     },  
     } 
    }); 

    // Initialize the Bloodhound suggestion engine 
    tags.initialize(); 

    // Instantiate the Typeahead UI 
    $('#search-tags').typeahead(null, { 
     displayKey: 'value', 
     source: tags.ttAdapter(), 
     hint: true, 
     highlight: true 
    }); 
+1

嘗試從瀏覽器的localStorage中刪除條目並重新開始。 – mdarwi 2015-02-05 21:06:55

+0

嗯,我試着使用Chrome的「刪除最近的活動」,並沒有這樣做,但手動刪除本地存儲中的密鑰後,它的工作。發佈這個答案,我會標記爲正確的。 – schnauss 2015-02-05 21:15:38

回答

1

嘗試從瀏覽器的localStorage中刪除條目並重新開始。

1

默認情況下,Bloodhound對象在瀏覽器的本地存儲器中緩存預取數據,爲其分配1天的TTL(生存時間),並且在TTL過期之前不會對其進行重新驗證。當Bloodhound對象初始化時,可以更改默認設置「cache:false」和/或「ttl:1000」(毫秒)。

預取與緩存有關,但稍有不同,因爲預取的數據不受服務器發送的緩存控制標頭的影響。它也存在於LocalStorage中,而不是在瀏覽器的緩存中(這就是爲什麼硬重載或緩存清除不會導致它被重新獲取的原因)。

另一方面,遠程獲取的文件將根據Cache-Control標頭進行重新驗證。所以如果服務器允許的話,瀏覽器仍然可以緩存它們。但是,它們存儲在緩存中,而不是LocalStorage中。

LocalStorage中存在每個域的空間限制(請參閱What is the max size of localStorage values?),因此大的預取將失敗,但我不知道typeahead是否優雅地失敗(即使用數據,即使它不能存儲它)。