简体   繁体   English

Typeahead.js-使用一个远程源的多个数据集

[英]Typeahead.js - Multiple Datasets using one Remote Source

I'm currently using typeahead.js with the searchkick rails gem to search and autocomplete queries. 我目前正在将typeahead.js与searchkick rails gem一起用于搜索和自动完成查询。 With the necessary adjustments to my controller I am now searching for records across two models (Artists and Albums): 在对控制器进行必要的调整后,我现在在两个模型(艺术家和专辑)中搜索记录:

class SearchController < ApplicationController
  ...
  def autocomplete
    @results = Artist.search(params[:query], index_name: [Artist.searchkick_index.name, Album.searchkick_index.name], fields: [{ name: :word_start }], limit: 10)

    render json: @results
  end
end

resources :search, only: :index do
  collection do
    get :autocomplete
  end
end

Search suggestions for the two are retrieved as expected but I now need to group the two in their own templates. 两者的搜索建议已按预期方式检索,但我现在需要将这两个分组在各自的模板中。 This has led me to looking into using Multiple Datasets . 这使我开始考虑使用多个数据集

In my js file I'm retrieving results using the remote as suggested in this post : 在我的js文件中,我按照这篇文章中的建议使用remote检索结果:

remote: { 
  url: "/search/autocomplete?query=%QUERY",
  wildcard: "%QUERY" 
}

Unlike what I have, the docs suggest pulling from the model's index action by using the prefetch option: 与我所拥有的不同,文档建议使用prefetch选项从模型的index操作中退出:

prefetch: '../data/nhl.json'

Unfortunately configuring my js in this manner gives me nothing. 不幸的是,以这种方式配置我的js没有任何帮助。 Presumably I'd need an albums variable but would it not just be identical to how I have my artists variable setup? 大概我需要一个albums变量,但是它不仅仅与我的artists变量设置相同吗? How would I go about in configuring this based on the JS I have now? 如何基于现在拥有的JS进行配置?

$(function() {
  var artists = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace("artist"),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: { 
      url: "/search/autocomplete?query=%QUERY",
      wildcard: "%QUERY" 
    }
  });

  artists.initialize();

  $(".search").typeahead(null, {
    displayKey: "name",
    source: artists.ttAdapter(),
    templates: { 
      header: "<h3>Artists</h3>",
      suggestion: function(data) {
        return "<a href=" + data.url + ">"+ data.name +"</a>";
      }
    },
    {
     // Albums would go here
    }
  });
});

Returned JSON 返回的JSON

/* artists.json */

{
id: 1,
name: "50 Cent",
avatar: ...,
url: "/artists/1"
}

/* albums.json */

{
id: 1,
name: "Get Rich or Die Tryin'",
artwork: ...,
genre: "Hip-Hop/Rap",
release_date: ...,
url: "/albums/1"
}

Try using suggestion property of templates to filter displayed results using url of returned data , RegExp.prototype.test() 尝试使用templates suggestion属性来使用返回data url RegExp.prototype.test()过滤显示的结果

$(".search").typeahead(null, {
    displayKey: "name",
    source: artists.ttAdapter(),
    templates: { 
      // header: "<h3>Artists</h3>",
      suggestion: function(data) {
        var temp;
        if (/^\/albums/.test(data.url)) {
           // albums template
           // temp = "albums template"
        } else {
           // temp = "artists template"
        }
        return temp;
      }
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM