Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery autocomplete use multiple sources (local json api and jsonp)

Basically I have a hotel search engine and on top of the site there is a search field which must show autocomplete results. Results can be Hotels or Locations (cities). Comparable to Facebook autocomplete (searching can be persons, pages,...)

I started from the basic geonames jsuery example: http://jqueryui.com/autocomplete/#remote-jsonp

But I cannot figure out how to use my own JSON api for the hotels. I know I should merge my JSON hotels with the ones from geonames? Anyone who can show me a snippet of how to this?

like image 457
Rova Avatar asked Jan 16 '23 00:01

Rova


1 Answers

Simplest high level code should look like this, where requestFromSource1 is where you request geonames, requestFromSource2 is where you query your own autocomplete engine.

  $( "#city" ).autocomplete({
        source: function( request, response ) {
    var resultFromSource1 = null;
    var resultFromSource2 = null;
    var agregateResults = function(){
        if( resultFromSource1 && resultFromSource2){
            var result = resultFromSource1.concat(resultFromSource2);
            response(result);
        }
    }
    requestFromSource1(function( result ){
        resultFromSource1 = result;
        agregateResults();
    });
    requestFromSource2(function( result ){
        resultFromSource2 = result;
        agregateResults();
    });
        }
    });
});

The more complex case is merging by relevance score. I'm afraid that this note possible in your case.

like image 86
Night Warrier Avatar answered Jan 23 '23 05:01

Night Warrier