2015-07-04 28 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中讨论的相同方式进行了更正。