i'm new to angularjs world and i'm trying to get my head around how to run the whole factry method in angularjs synchronous.
factory.factory("Connections", function($rootScope, $http, $q) {
var deferred = $q.defer();
var connectionStatus;
console.log("start of factory.factory function");
$http.get('http://localhost:8088/ecus').success(function(response) {
deferred.resolve(response);
console.log(" connection status: " + response.status);
connectionStatus = 'available';
})
.error(function(errResponse) {
deferred.reject({
message: "No connection"
});
// return deferred.promise;
connectionStatus = 'not available';
console.log(connectionStatus);
});
console.log("end of the factory.factory function");
return [{
id: 'conID1',
pathToImage: 'images/default_test /eBikeImages/Bionx_creme.png',
title: 'connection 1',
status: connectionStatus,
visible: true,
favorite: true
}];
});
the idea is to get teh value of the connectionStatus from URL call and use it in return value which is the JSON object. the problem is that the factory is being called but the value for the connectionsStatus is not being assigned and the Json is being returned with the undefined value for the connectionStatus. in the console I see that the log for "start of factory" and "end of factory" is being executed before the http calls ! any help would be appreciated :)