我正在使用基于 jQuery 的select2替换组合框,并且我必须定义一个回调来处理我从 json rest web 服务接收到的数据。
问题是,在同一个回调中,我必须发出另一个 GET 请求来获取匹配记录的总数,以便 select2 可以决定是否必须加载更多结果(它具有无限滚动功能)
代码是这样的:
$("#country").select2({
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
url: 'http://localhost:9000/api/countries',
dataType: 'json',
data: function(term, page) {
return {
filter: term,
page: page,
len: 10
};
},
results: function(data, page) {
return {
results: data, more: ????
};
}
}
});
问题是我不知道如何发出异步请求(我正在发出跨域请求,并且文档说在这种情况下不支持异步)并等待它完成,然后再从结果回调中返回。
select2 页面中的示例如下所示:
results: function (data, page) {
var more = (page * 10) < data.total; // whether or not there are more results available
// notice we return the value of more so Select2 knows if more results can be loaded
return {results: data.movies, more: more};
}
问题是我的 Web 服务返回来自不同端点的记录总数,所以我必须发出另一个请求,如下所示:http://localhost:9000/api/countries?filter=term
任何的想法?