2012-11-18 23 views
4

基本上我有一個酒店搜索引擎,並且在網站頂部有一個必須顯示自動完成結果的搜索字段。結果可以是酒店或地點(城市)。堪比Facebook的自動完成(搜索可能會人員,網頁,...)jquery自動完成使用多個來源(本地json api和jsonp)

我從基本GEONAMES jsuery例子開始:http://jqueryui.com/autocomplete/#remote-jsonp

但我無法弄清楚如何使用我自己的JSON API的酒店。我知道我應該將我的JSON酒店與來自geonames的酒店合併?任何人都可以向我展示如何做到這一點?

+0

將一個數組內的所有數據源合併,然後使用 – sdespont

回答

9

最簡單的高級代碼應該如下所示,其中requestFromSource1是您請求geonames的位置,requestFromSource2是您查詢自己的自動填充引擎的位置。

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

更復雜的情況是通過相關性分數合併。恐怕你的情況可能會發生這樣的情況。

+0

設置您的自動完成數據源非常好的示例以及我所需要的。 –