Jquery autocomplete uses multiple sources (local json api and jsonp)

Basically, I have a hotel search engine, and on the top of the site there is a search field that should show autocomplete results. The results may be hotels or places (cities). Comparable to Facebook autocomplete (search can be persons, pages, ...)

I started with a basic jonery geonames example: http://jqueryui.com/autocomplete/#remote-jsonp

But I can not understand how to use my own JSON api for hotels. I know that I have to combine my JSON hotels with those of geonames? Anyone who can show me a snippet of how to do this?

+4
source share
1 answer

The simplest high-level code should look like where requestFromSource1 is where you request geodata, requestFromSource2 is where you request your own autostart.

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

A more complex case is combined by relevance assessment. I am afraid that this note is possible in your case.

+9
source

Source: https://habr.com/ru/post/1446802/


All Articles