Skip to content

Commit

Permalink
Merge pull request #4 from AlexYankee/master
Browse files Browse the repository at this point in the history
Multiple promises in getCenterCoordinates can cause race condition
  • Loading branch information
tulov committed Jan 11, 2014
2 parents e52488f + 7b071b4 commit e2d0688
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions example/ya-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ angular.module('yaMap',[]).
yaAfterInit:'&'
},
compile: function(tElement) {
var childNodes = tElement.contents();
var childNodes = tElement.contents(),
centerCoordinatesDeferred = null;
tElement.html('');
return function(scope, element,attrs) {
var getEvalOrValue = function(value){
Expand All @@ -215,7 +216,9 @@ angular.module('yaMap',[]).
}
};
var getCenterCoordinates = function(center){
var deferred = $q.defer();
if(centerCoordinatesDeferred)
centerCoordinatesDeferred.reject();
centerCoordinatesDeferred = $q.defer();
var result;
if(!center){
//устанавливаем в качестве центра местоположение пользователя
Expand All @@ -225,27 +228,27 @@ angular.module('yaMap',[]).
}else{
result = [ymaps.geolocation.latitude, ymaps.geolocation.longitude];
}
deferred.resolve(result);
centerCoordinatesDeferred.resolve(result);
});
}else if(angular.isArray(center)){
deferred.resolve(center);
centerCoordinatesDeferred.resolve(center);
}else if(angular.isString(center)){
//проводим обратное геокодирование
mapApiLoad(function(){
ymaps.geocode(center, { results: 1 }).then(function (res) {
var firstGeoObject = res.geoObjects.get(0);
result = firstGeoObject.geometry.getCoordinates();
scope.$apply(function(){
deferred.resolve(result);
centerCoordinatesDeferred.resolve(result);
});
}, function (err) {
scope.$apply(function(){
deferred.reject(err);
centerCoordinatesDeferred.reject(err);
});
});
});
}
return deferred.promise;
return centerCoordinatesDeferred.promise;
};
var zoom = Number(attrs.yaZoom),
behaviors = attrs.yaBehaviors ? attrs.yaBehaviors.split(' ') : ['default'];
Expand Down

0 comments on commit e2d0688

Please sign in to comment.