2015-04-27 12 views
6

Sto usando Bloodhound con un'API remota e ho bisogno di trasformare il risultato restituito dall'API remota.Bloodhound.js: Trasforma i dati restituiti da una fonte remota?

L'URL dell'API è https://www.googleapis.com/books/v1/volumes?q=quilting che restituisce un oggetto con una proprietà items che è un elenco. Devo restituire quell'elenco a Typeahead, piuttosto che all'oggetto di livello superiore.

I documenti Bloodhound dicono che there is a transform function that is supposed to do this, ma non riesco a farlo funzionare.

Ecco il mio codice:

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() 
}); 

E un JSFIddle qui: http://jsfiddle.net/2Cres/46/

Questo non funziona, perché ho bisogno di alimentare la lista items nella UI typeahead, ma questo non sembra stare succedendo.

risposta

6

provare a spostare trasformare all'interno l'opzione remota, in questo modo:

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

@Kristoffer Sall-Storgaard, in realtà si tratta di una risposta, ma, può non essere in forma chiara, spiacente per quello – skazska

+0

ho modificato la tua risposta , se non è un miglioramento, sentitevi liberi di tornare indietro. –

+0

@Kristoffer Sall-Storgaard grazie. – skazska

Problemi correlati