Skip to content

Allow custom "remote" for bloodhound. #907

Open

Description

@kevincox

Currently Bloodhound forces request to be made with jQuery.ajax. It would be nice if alternative "fetchers" could be plugged in.

The advantage would be flexibility and the options to get results from other places such as arbitrary javascript api's or non-json endpoints.

Concept API:

var e = new Bloodhound({
    /* ... */
    remote: function(tokens, callback){
        var datums = /* whatever you want. */
        callback(datums);
    },
});

Activity

  1. DavidMoore commented on Jul 31, 2014

    @DavidMoore

    Hi Kevin.

    I was needing this too; I had a dig around in the source and found a possible extension point.

    This might be intended and is just undocumented; or they will implement something else in the future.

    What you do is implement your own transport callback in the bloodhound remote options, then fire the error or success functions passed in by Bloodhound so that it can continue with the response.

    In my example, I've just implemented what the default transport does (call Jquery ajax).

    var test = new Bloodhound({
        remote: {
            transport: function (url, options, onSuccess, onError) {
    
                // Here, maybe log that we're about to look up remote data.
    
                // Here's where you'd do your custom lookup; for this example, we'll just use Jquery ajax as Bloodhound does.
                $.ajax(url, options).done(done).fail(fail).always(always);
    
                function done(data, textStatus, request) {
                    // Don't forget to fire the callback for Bloodhound
                    onSuccess(data);
                }
    
                function fail(request, textStatus, errorThrown) {
                    // Don't forget the error callback for Bloodhound
                    onError(errorThrown);
                }
    
                function always() {
    
                }
            }
        }
    });
  2. DavidMoore commented on Jul 31, 2014

    @DavidMoore

    I've actually gone ahead and updated the Bloodhound API docs with the above, and put in a pull request: #921

  3. kevincox commented on Jul 31, 2014

    @kevincox
    Author

    Thanks @DavidMoore that is exactly what I am looking for.

  4. added this to the v1.0.0 milestone on Aug 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions