admin管理员组文章数量:1415664
I am trying to use jQuery UI Autoplete feature in order to make a wrapper of Google Autoplete Service(because I just want to restrict some of the results returned by Google in a manner that is not possible through Google API).
Suppose I have this code:
$("#address").autoplete({
source: function(request, response){
autoCompleteService = new google.maps.places.AutopleteService();
autoCompleteService.getQueryPredictions({input: request.term }, autopleteCallback);
//I should somewhere call the "response" object with desired suggestions as arguments
},
minLength: 5,
});
The problem is that jQuery UI Autoplete forces me to call the "response" object(which is actually a function) with the suggestions I would like to show to the user as parameters.
But, on the other hand, Google API forces me to define a callback function(in my case 'autopleteCallback') to whom it gives the requested suggestions as parameters after it's done.
Of course, I can't call the 'response' object inside the 'autopleteCallback' function, and I can't call the response object just after this line either:
autoCompleteService.getQueryPredictions({input: request.term }, autopleteCallback);
Because JS is async and I couldn't be sure that I get something in let's say: a global variable that I use in order to pass the results.
What would be the solution for that? Is there a well-known JS design pattern for a problem like this?
I am trying to use jQuery UI Autoplete feature in order to make a wrapper of Google Autoplete Service(because I just want to restrict some of the results returned by Google in a manner that is not possible through Google API).
Suppose I have this code:
$("#address").autoplete({
source: function(request, response){
autoCompleteService = new google.maps.places.AutopleteService();
autoCompleteService.getQueryPredictions({input: request.term }, autopleteCallback);
//I should somewhere call the "response" object with desired suggestions as arguments
},
minLength: 5,
});
The problem is that jQuery UI Autoplete forces me to call the "response" object(which is actually a function) with the suggestions I would like to show to the user as parameters.
But, on the other hand, Google API forces me to define a callback function(in my case 'autopleteCallback') to whom it gives the requested suggestions as parameters after it's done.
Of course, I can't call the 'response' object inside the 'autopleteCallback' function, and I can't call the response object just after this line either:
autoCompleteService.getQueryPredictions({input: request.term }, autopleteCallback);
Because JS is async and I couldn't be sure that I get something in let's say: a global variable that I use in order to pass the results.
What would be the solution for that? Is there a well-known JS design pattern for a problem like this?
Share Improve this question asked Nov 10, 2012 at 16:14 TerenteTerente 912 silver badges5 bronze badges1 Answer
Reset to default 7You, sir, are a genius to bine the two frameworks! So I'll share the solution that I have:
$("#search").autoplete({
source: function (request, response) {
autoCompleteService.getQueryPredictions({ input: request.term }, function (predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
response($.map(predictions, function (prediction, i) {
return {
label: prediction.description,
value: prediction.description
}
}));
});
},
select: function (event, ui) {
var value = ui.item.value;
searchMap(value);
}
});
Of course, I have an actual function that does the proper Place lookup (searchMap()
) that takes in a term. The only realistic way to do this in order to have a proper autopleteCallback
AND response
handler for jQuery UI autoplete is to keep the callback implementation inline. It sucks if you want to do this in more than one place but I haven't thought of anything better. Yet...
版权声明:本文标题:javascript - Wrap Google Places Autocomplete Service through jQuery UI Autocomplete feature - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745219253a2648313.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论