我正在使用Bloodhound和远程API,我需要转换从远程API返回的结果.
API URL为https://www.googleapis.com/books/v1/volumes?q=quilting,返回具有项属性(列表)的对象.我需要将该列表返回给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/
这不起作用,因为我需要将项目列表提供给预先输入的UI,但这似乎并没有发生.
解决方法
尝试在远程选项中移动transform,如下所示:
remote { url:"fdsfds",transform: function (response){...} }