Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom "remote" for bloodhound. #907

Open
kevincox opened this issue Jul 15, 2014 · 3 comments
Open

Allow custom "remote" for bloodhound. #907

kevincox opened this issue Jul 15, 2014 · 3 comments
Milestone

Comments

@kevincox
Copy link

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);
    },
});
@DavidMoore
Copy link

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() {

            }
        }
    }
});

@DavidMoore
Copy link

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

@kevincox
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants