I have a configs object. For each object in configs, I am looping and performing a call using $http.get (which is in SPARQLService). The configs object contains different object and thus doing different calls on different URLs. Within that loop, whenever I am getting the promise from the $http.get, in the then function I am updating an object found in $rootScope. The problem I am getting is that the configs contains different object but the object i am updating in rootScope all contains results for a similar object from the configs. A sample code:
for (var config in configs ){
var newConfig = configs[config];
var data = SPARQLService.query(newConfig["endpointURL"],encodeURIComponent(newConfig["SPARQLQuery"]));
var bindings;
data.then(function (answer){
sparql_result = answer.data;
bindings = answer.data.results.bindings;
//creating the markers
markers = [];
for (var binding in bindings){
currentBind = bindings[binding];
var latitude = currentBind[newConfig["lat"]].value;
var longitude = currentBind[newConfig["long"]].value;
var currentMarker = L.marker([latitude, longitude]);
currentMarker.bindPopup(Utilities.generateDescription(newConfig["desc"],currentBind));
markers.push(currentMarker);
}
$rootScope.config[newConfig["groupName"]] = newConfig;
$rootScope.layers[newConfig["groupName"]] = L.layerGroup(markers);
console.log($rootScope.config);
},
function (error){
});
}
var newConfigbeing declared outside the function that references it in the loop.newConfigwill be updated on each iteration of the loop but the reference to it is used in the functions that are asynchronously triggered, believe they will all be referencing the samenewConfigat the time they execute... would verify this with the debugger with breakpoints where the newConfig is used to assign things to $rootScope.