有人能帮我理解下面的代码吗?我在这里找到它。
它利用了JQuery UI Autocomplete的远程源。我已经尽可能地注释了代码,并提出了一个更精确的问题。
$( "#city" ).autocomplete({
source: function( request, response ) {
//request is an objet which contains the user input so far
// response is a callback expecting an argument with the values to autocomplete with
$.ajax({
url: "http://ws.geonames.org/searchJSON", //where is script located
dataType: "jsonp", //type of data we send the script
data: { //what data do we send the script
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function( data ) { //CONFUSED!
response(
$.map(
data.geonames, function( item ) {
return {
label: item.name+(item.adminName1 ? ","+item.adminName1:"")+","+item.countryName,
value: item.name
}
}
)
);
}
});
}
});
正如你所看到的,我不理解success函数和响应回调的用法。
我知道success函数字面上是一个AJAX选项,在AJAX查询返回时被调用。在这种情况下,它似乎封装了对响应回调的调用?响应回调定义在哪里?我认为按照回调的定义,它应该自己被调用吧?
谢谢!