我使用的是带远程API的血狗,我需要转换从远程API返回的结果。
API是https://www.googleapis.com/books/v1/volumes?q=quilting,它返回一个具有items属性的对象,该属性是一个列表。我需要将该列表返回到Typeahead,而不是顶级对象。
血猎犬的医生说 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列表输入到预先输入的UI中,但这似乎并没有发生。
发布于 2015-04-27 12:14:12
尝试在远程选项中移动转换,如下所示:
remote {
url:"fdsfds",
transform: function (response){...}
}https://stackoverflow.com/questions/29895014
复制相似问题