JQuery UI 自动完成语法
有人可以帮我理解下面的代码吗?我在这里找到了它。
它利用远程源的 JQuery UI 自动完成功能。我已尽我所能对代码进行了评论,后面还有一个更精确的问题。
$( "#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 函数文字是一个 AJAX 选项,当 AJAX 查询返回时会调用它。这样的话,好像封装了一个响应回调的调用?哪个是在哪里定义的?我认为根据回调的定义,它应该被单独调用?
谢谢!
Can someone help me understand the following code? I found it here.
It takes advantage of the JQuery UI Autocomplete with a remote source. I've commented the code as best I can and a more precise question follows it.
$( "#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
}
}
)
);
}
});
}
});
As you can see, I don't understand the use of the success function and the response callback.
I know the success function literal is an AJAX option which is called when the AJAX query returns. In this case, it seems to encapsulate a call to the response callback? Which is defined where? I thought by definition of a callback, it should be called on its own?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档(“概述”页面)定义的
response
对象:因此,“response”参数实际上是一个回调,必须在 ajax 检索自动完成项成功时调用它。
由于您的数据将通过 AJAX 返回,因此您的代码必须手动更新小部件。 jQueryUI 提供一个参数作为函数,以便您的代码可以通过调用该函数来进行更新。
您可以看到用于
source
选项的函数声明中定义的response
对象:您甚至可以将 AJAX 调用从等式中剔除,并执行如下操作:
将立即使用小部件的标签/值对数组调用
response
回调。The
response
object as defined by the documentation ("Overview" page):so, the 'response' argument is actually a callback, which must be called upon success of the ajax retrieval of autocomplete items.
Since your data will come back via AJAX, your code must update the widget manually. jQueryUI provides an argument as a function so that your code can do that update by calling the function.
You can see the
response
object defined in the declaration of the function used for thesource
option:You could even take the AJAX call out of the equation and do something like this:
Would immediately call the
response
callback with an array of label/value pairs for the widget.