【问题标题】:Typeahead.js - Multiple Datasets using one Remote SourceTypeahead.js - 使用一个远程源的多个数据集
【发布时间】:2016-04-01 01:27:10
【问题描述】:

我目前正在使用 typeahead.js 和 searchkick rails gem 来搜索和自动完成查询。通过对我的控制器进行必要的调整,我现在正在搜索两个模型(艺术家和专辑)的记录:

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

按预期检索这两者的搜索建议,但我现在需要将这两者分组到各自的模板中。这促使我考虑使用 Multiple Datasets

在我的 js 文件中,我正在使用 remote 检索结果,正如 post 中所建议的那样:

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

与我所拥有的不同,文档建议使用 prefetch 选项从模型的索引操作中提取:

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

不幸的是,以这种方式配置我的 js 并没有给我任何东西。大概我需要一个albums 变量,但它不仅与我的artists 变量设置相同吗?我将如何根据我现在拥有的 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
    }
  });
});

返回的 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"
}

【问题讨论】:

  • 能否包含来自每个“艺术家”、“专辑”json 的返回数据示例?一种方法是使用返回数据的属性过滤显示在templates 的结果
  • 更新了我的答案。这就是你要找的东西吗?
  • 是的。如果返回对象的url 属性分别以"/artists""/albums" 开头,则可以过滤使用这些属性显示的template
  • 你能举个例子吗?理想情况下,我可以在我的序列化程序中包含对象的类,这样它就可以读取类似data.class =&gt; "artist"

标签: javascript ruby-on-rails json typeahead.js searchkick


【解决方案1】:

尝试使用templatessuggestion 属性来过滤使用返回的url 的显示结果dataRegExp.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;
      }
    }
  });
});

【讨论】:

  • 在为每个查询查找使用正则表达式时,您是否对性能有任何影响?
  • @CarlEdwards 不应影响性能。也可以使用 String.prototype.slice()String.prototype.match()
  • 好的,谢谢。另外,您将如何有条件地设置 header,因为它在 suggestion 函数范围之外并且无权访问 data 对象?
  • @CarlEdwards 最初可以将标题 h3 加载为空元素,也许提供一个唯一的 className ,然后在 suggestion 内,一旦确定了 data 的类型,附加适当的 htmlh3 。或者,创建h3 元素作为temp 的第一个元素
猜你喜欢
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多