简体   繁体   English

javascript / jQuery中的回调函数参数

[英]callback function argument in javascript / jQuery

Trying to go through a book to learn jQuery ui. 试图读一本书来学习jQuery ui。 I run into this code 我碰到了这段代码

 $("#autocomplete").autocomplete({
     minLength: 2,
     source: function (request, response) {
         $.getJSON("https//api.github.com/search/repositories", {
             q: request.term + " in:name"
         })
             .then(function (data) {
             var matches = $.map(data.items, function (repo) {
                 return repo.full_name;
             });
             response(matches);
         });
     }
 });

In the .then(function(data){}), is the data object passed by the result of $.getJSON() function? 在.then(function(data){})中,$ .getJSON()函数的结果传递数据对象吗? I checked jQuery API. 我检查了jQuery API。 I think in this case I am dealing with doneFilter callback but the API didn't mention any default object that is passed in the function. 我认为在这种情况下,我正在处理doneFilter回调,但API并未提及函数中传递的任何默认对象。 Can anyone explain more about this behavior? 谁能解释更多有关此行为的信息? And why the data object contains a items property? 以及为什么数据对象包含项属性?

You are dealing with a promise object. 您正在处理一个promise对象。 As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest object. 从jQuery 1.5开始,所有jQuery的Ajax方法都返回XMLHTTPRequest对象的超集。 This jQuery XHR object, or "jqXHR," returned by $.getJSON() implements the Promise interface, giving it all the properties, methods, and behavior of a Promise. $ .getJSON()返回的此jQuery XHR对象或“ jqXHR”实现Promise接口,为其提供Promise的所有属性,方法和行为。

so .then() will get called after the completion of async call and it will use the success response object in data as an input. 因此.then()将在异步调用完成后被调用,并将使用数据中的成功响应对象作为输入。

FOR MORE DETAILS ON PROMISE 有关承诺的更多详细信息

http://davidwalsh.name/write-javascript-promises http://davidwalsh.name/write-javascript-promises

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM