Currently, I am loading two JSON files using separate services in the following manner:
app.factory('AventosService', function($rootScope, $http)
{
var data = [];
return {
promise: null,
loadAventosJson: function()
{
this.promise = $http.get('resources/json/aventos.json',{timeout:20000}).then(
function(response)
{
data = response.data;
$rootScope.$broadcast('AventosJsonLoaded');
},
function(data)
{
log('ERROR: ' + data.status);
})
},
getAventosJson: function()
{
if (!data.length && !this.promise) this.loadAventosJson();
return data;
}
}
});
app.factory('PartsService', function($rootScope, $http)
{
var data = [];
return {
promise: null,
loadPartsJson: function()
{
this.promise = $http.get('resources/json/part_numbers.json',{timeout:20000}).then(
function(response)
{
data = response.data;
$rootScope.$broadcast('PartsJsonLoaded');
},
function(data)
{
log('ERROR: ' + data.status);
})
},
getPartsJson: function()
{
if (!data.length && !this.promise) this.loadPartsJson();
return data;
}
}
});
To retrieve the data from the services, I use the following code snippets:
$scope.aventosJson = AventosService.getAventosJson();
and
$scope.partsJson = PartsService.getPartsJson();
I then verify if both events were triggered. These events are AventosJsonLoaded
and PartsJsonLoaded