2015-07-04 27 views
4

我使用Bloodhound從數據庫中獲取數據,然後twitter鍵入以顯示搜索框下方的選項。Twitter typeahead只顯示血獵犬返回的一些項目

目前,獵犬部分正在查找所需的物體,但前部並未顯示它們。

var artist_retriever = new Bloodhound({ 
    // turns input query into string of tokens to send to database. 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 

    remote: { 
       // URL to fetch information from 
       url: "/artists?query=%QUERY", 
       wildcard: '%QUERY', 
       // Manipulate the array of artists returned, for display to user. 
       transform: function(array_of_artists){ 
          // array of artists is returned from DB. 
          // Put each artist into a readable string 
          array_of_artists = create_artist_descriptions(array_of_artists) 

          console.log(array_of_artists) 
          // Returns correctly: 
          // [ 
          // { artist: "Joe" }, 
          // { artist: "Bob" }, 
          // { artist: "Smith" }, 
          // { artist: "Tom" }, 
          // ] 

          return array_of_artists 
          } 
      }, 

    // turns return value into a string of results, with this 'key' before each result. 
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('artist'), 


    }); 

// display: 



// instantiate the typeahead UI 
    // https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md 
    searcher = $('.typeahead').typeahead(
    // options: 
    { 
     hint: false 
    }, 
    // datasets: 
    { 
     // Where to get data: User the bloodhound suggestion engine: 
     source: artist_retriever.ttAdapter(), 
     // Which attribute of each result from the database should be shown: 
     displayKey: 'artist', 
     templates: { 
        notFound: new_artist_option_template(), 
        footer: new_artist_option_template() 
       } 
    } 
) 

更新

事實證明,有一個在預輸入一個奇怪的錯誤。它似乎只能與「limit」屬性一起使用,最多設置爲4.如果將「limit」設置爲5,則typeahead不會提供任何內容。

searcher = $('.typeahead').typeahead(
    // options: 
    { 
     hint: false 
    }, 
    // datasets: 
    { 
     // Where to get data: User the bloodhound suggestion engine: 
     source: artist_retriever.ttAdapter(), 
     // Which attribute of each result from the database should be shown: 
     displayKey: 'signature', 
     limit: 4, // This can do a max of 4! Odd. 
     templates: { 
        notFound: new_artist_option_template(), 
        footer: new_artist_option_template() 
       } 
    } 
+0

你解決了嗎?目前正在研究類似的東西,並且發現預先學習是一種學習曲線。 –

+0

我剛剛遇到了同樣的問題,並將結果限制爲4次。謝謝,順便說一句。然而,你是否提前鍵入了這個錯誤,或者是否有一些我們可以跟蹤的錯誤參考號。最後,如果你有時間,而且你確實找到了這個問題的原因,你能否在評論中提到我,以便我得到有關此事的通知。謝謝。 – Rash

回答

2

此問題已得到解決。請直接參閱更新2

我在這JSFIDDLE轉載了這個問題。

正如你所說,它是一個錯誤。您還報告說,如果您使用limit:4,此錯誤消失。

其實在我的最後,還是在FIDDLE,我遇到過這個問題的時候出現number of results returned = value in limit

要在FIDDLE中測試此問題,請執行以下操作: 注意:搜索1947時返回恰好5行。

當限制設置爲4時: 搜索1947年返回4條結果。

當限制設置爲5時: 搜索1947時不返回任何內容。

當限制設置爲6時: 搜索1947返回一個1結果 - 第一個結果。


因此,如果你一直設置爲1小於結果的實際數量的限制返回,那麼這將繼續工作。

我也曾在自己的github page提出這個問題。我會跟蹤這個問題,並會根據需要不斷更新此答案。

更新1:

發現一個similar question on SO here「LucianoGarcíaBes」似乎已經找到了解決辦法。請在那裏指導所有提示。

基本上,他說:

它計數呈現提示的數量附加在他們面前,所以 如果提示的數量等於限制它會追加一個空數組。

爲了防止這一點,我剛換線1723和1724,所以它看起來是這樣的:

that._append(query, suggestions.slice(0, that.limit - rendered)); 
rendered += suggestions.length; 

更新2:

這個問題已經固定在拉1212。關閉我們自己的問題1312。該錯誤已按照update 1中討論的相同方式進行了更正。