Skip to content

Commit 807b379

Browse files
committed
feat(autocomplete): handle async loading of google maps API
1 parent b916c7b commit 807b379

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

dist/autocomplete.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/autocomplete.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ angular.module('google.places', [])
1515
* Note: requires the Google Places API to already be loaded on the page.
1616
*/
1717
.factory('googlePlacesApi', ['$window', function ($window) {
18-
if (!$window.google) throw 'Global `google` var missing. Did you forget to include the places API script?';
18+
if (!$window.google) {
19+
console.info('Global `google` var missing. Did you forget to include the places API script?');
20+
}
1921

2022
return $window.google;
2123
}])
@@ -48,8 +50,8 @@ angular.module('google.places', [])
4850
down: 40
4951
},
5052
hotkeys = [keymap.tab, keymap.enter, keymap.esc, keymap.up, keymap.down],
51-
autocompleteService = new google.maps.places.AutocompleteService(),
52-
placesService = new google.maps.places.PlacesService(element[0]);
53+
_autocompleteService = null,
54+
_placesService = null;
5355

5456
(function init() {
5557
$scope.query = '';
@@ -62,6 +64,14 @@ angular.module('google.places', [])
6264
initNgModelController();
6365
}());
6466

67+
function autocompleteService() {
68+
return _autocompleteService || new google.maps.places.AutocompleteService();
69+
}
70+
71+
function placesService() {
72+
return _placesService || new google.maps.places.PlacesService(element[0]);
73+
}
74+
6575
function initEvents() {
6676
element.bind('keydown', onKeydown);
6777
element.bind('blur', onBlur);
@@ -164,7 +174,7 @@ angular.module('google.places', [])
164174
});
165175
});
166176
} else {
167-
placesService.getDetails({ placeId: prediction.place_id }, function (place, status) {
177+
placesService().getDetails({ placeId: prediction.place_id }, function (place, status) {
168178
if (status == google.maps.places.PlacesServiceStatus.OK) {
169179
$scope.$apply(function () {
170180
$scope.model = place;
@@ -188,7 +198,7 @@ angular.module('google.places', [])
188198
$scope.query = viewValue;
189199

190200
request = angular.extend({ input: viewValue }, $scope.options);
191-
autocompleteService.getPlacePredictions(request, function (predictions, status) {
201+
autocompleteService().getPlacePredictions(request, function (predictions, status) {
192202
$scope.$apply(function () {
193203
var customPlacePredictions;
194204

0 commit comments

Comments
 (0)