twitter / typeahead.js

typeahead.js is a fast and fully-featured autocomplete library
http://twitter.github.io/typeahead.js/
MIT License
16.52k stars 3.2k forks source link

Allow remote to be a callback #8

Closed koen-serry closed 11 years ago

koen-serry commented 11 years ago

If the remote returns something where the dataset is nested, you can't extract the dataset from it. It would also allow to customise the call to the backend if some extra headers were to be set.

jharding commented 11 years ago

I like this idea, what if we supported something like the this:

$('.typeahead').typeahead({
  name: 'remoteTest',
  remote: {
    url: 'http://example.com?q=%QUERY',
    beforeSend: function(jqXhr, settings) {
       // set headers      
    },
    success: function(resp, status, jqXhr) {
      // return nested data from response
    }
  }
});

Would that offer the flexibility you're looking for?

adamtoth commented 11 years ago

+1 for this.

I'd love to use this in SharePoint, but the JSON data I get back from its REST services are heavily nested, and I'd need to translate it to a dto to work with the data set expected format.

koen-serry commented 11 years ago

I'm still wondering why the separation between local and remote. Can't we just create a datasource property, if it's a function it uses the contents of the return (possibly ajax) in all other cases it just uses the object.

eads commented 11 years ago

I hate to be annoying and revisit an old issue, but I am running into a similar issue and this doesn't quite address it. For my use case, I could really use a remote or another way of specifying a datasource that as @koen-serry says "if it's a function it uses the contents of the return (possibly ajax)".

I'm using typeahead JS in a scenario where I have a remote data source that I must use to get some event listing data. However, the API's search feature is so screwed up that we're using LunrJS to handle all search client side.

What I'd love to see is something along these lines:

var searchIndex = lunr( ... );

$('.typeahead').typeahead({
  name: 'localSearchTest',
  remote: function(input) {
    return searchIndex.search(input);
  }
})

In real life, you'd need to do a little more processing with the search results to get a list that was useful for typeaheadJS, but the fundamental thing is some way to support a function that returns a list in addition to a static list and a remote endpoint.

(P.S. Thanks for the fantastic library. This thing is amazing!)

rgravina commented 10 years ago

I'm also wondering how to work around this issue in order to use Bloodhound prefetching etc.

In my case, remote data comes from an API call with a success callback function.

Without Bloodhound, you can use a callback using the source parameter and everything is fine. With Bloodhound however, local expects an array of values to be returned, and remote expects a URL.

Am I missing something obvious?

aribornstein commented 8 years ago

Was this resolved if so how do I access the nested data?