Below is the functionality my code achieves currently
- Get the customer site . If customer site exists then check if latitude and longitude exist and if all is true then open the Locator with given latitude and Longitude.
If customer site exists but Latitude and Longitude does not exist but customer city , state and zip code exist so geocode the address using remoting javascript and open the Locator after geocoding the site and retreiving the latitude and Longitude with it.
If the customer site does not exist and Latitude and Longitude and also no city or state exist and it is not possible to geocode the site then open default location.
Below code achieves that in angular controller but as you can see it has many callbacks and if else statements. My main objective is to reduce the callbacks and if else statements and make it more faster and modular. How can I achieve this in angular. any help would be appreciated. My code I wrote is given below
var pageRef = "/apex/LeadLocator?";
$scope.CustomerSite.loaded = false;
console.log( "5-"+$scope.CustomerSite );
if($scope.CustomerSite && $scope.CustomerSite.lat && $scope.CustomerSite.lon && $scope.CustomerSite.lat !== 0 && $scope.CustomerSite.lon !== 0)
{
pageRef += 'lat='+$scope.CustomerSite.lat;
pageRef += '&lon='+$scope.CustomerSite.lon;
jQ("#handlebars-action-geocode").html( jQ("#task-completed-template").html() );
// jQ("#handlebars-action-locator").html( jQ("#task-executing-template").html() );
//$window.location.href = pageRef;
}
else if( $scope.CustomerSite && ( !$scope.CustomerSite.lat || !$scope.CustomerSite.lon || ($scope.CustomerSite.lat == 0 && $scope.CustomerSite.lon == 0)))
{
// Execute additionl geoCoding on this site
if($scope.CustomerSite.SiteStreet && $scope.CustomerSite.SiteCity && $scope.CustomerSite.SiteCountryCode && $scope.CustomerSite.SitePostalCode )
{
LeadLocatorPreProcessorControllerV2.geoCodeSite($scope.CustomerSite.SiteStreet + ' ,' + $scope.CustomerSite.SiteCity + ' ,' + $scope.CustomerSite.SiteCountryCode + ' ,' + $scope.CustomerSite.SitePostalCode, function(result, event){
if (event.status) {
if(result && result.AddressLat && result.AddressLon)
{
LeadLocatorPreProcessorControllerV2.UpdateAddrGeoCodingInfo( $scope.CustomerSite.AddressID, result.AddressLat, result.AddressLon, function(){
if (event.status) {
pageRef += 'lat='+result.AddressLat;
pageRef += '&lon='+result.AddressLon;
jQ("#handlebars-action-geocode").html( jQ("#task-completed-template").html() );
// jQ("#handlebars-action-locator").html( jQ("#task-executing-template").html() );
//$window.location.href = pageRef;
} else if (event.type === 'exception') {
document.getElementById("responseErrors").innerHTML = event.message + "<br/>\n<pre>" + event.where + "</pre>";
} else {
document.getElementById("responseErrors").innerHTML = event.message;
}
});
}
} else if (event.type === 'exception') {
document.getElementById("responseErrors").innerHTML = event.message + "<br/>\n<pre>" + event.where + "</pre>";
} else {
document.getElementById("responseErrors").innerHTML = event.message;
}
});
}
}
};