【发布时间】: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