【问题标题】:Bloodhound.js: Transform the data returned by a remote source?Bloodhound.js:转换远程源返回的数据?
【发布时间】:2015-04-27 11:55:17
【问题描述】:

我正在使用带有远程 API 的 Bloodhound,我需要转换从远程 API 返回的结果。

API URL 是https://www.googleapis.com/books/v1/volumes?q=quilting,它返回一个具有items 属性的对象,该属性是一个列表。我需要将该列表返回给 Typeahead,而不是顶级对象。

Bloodhound 文档说 there is a transform function that is supposed to do this,但我无法让它工作。

这是我的代码:

var books = new Bloodhound({
  datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.num); },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: 'https://www.googleapis.com/books/v1/volumes?q=quilting'
  },
  transform: function(response) {
    console.log('transform', response);
    return response.items;
  }
});
books.initialize();

// instantiate the typeahead UI
$('#myTextBox').typeahead(null, {
  displayKey: function(suggestion) {
    console.log('suggestion', suggestion);
    return suggestion.volumeInfo.title + suggestion.volumeInfo.publishedDate;
  },
  source: numbers.ttAdapter()
});

这里还有一个 JSFIddle:http://jsfiddle.net/2Cres/46/

这不起作用,因为我需要将 items 列表输入到 typeahead UI,但这似乎没有发生。

【问题讨论】:

    标签: javascript bloodhound


    【解决方案1】:

    尝试在远程选项中移动变换,如下所示:

    remote {
      url:"fdsfds",
      transform: function (response){...}
    }
    

    【讨论】:

    • @Kristoffer Sall-Storgaard,实际上这是一个答案,但是,可能形式不明确,抱歉
    • 我编辑了您的答案,如果没有改进,请随时回滚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-16
    • 2011-04-15
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    相关资源
    最近更新 更多