Here's a question regarding the best practices for organizing code.
I'm fetching data from an API using $resource and I need to manipulate it before displaying it on the view.
My dilemma is at which stage should I process the data? My current belief is that it should be handled in the service, but I also prefer handling the actual AJAX call within the controller.
Currently, I am injecting the service and utilizing functions like CdnService.sumOfVolumeRequest(response) to process the data.
Is my approach correct or is there a more effective way?
Service:
function updateVolumeRequest() {
var params = {
metric: "size",
tStart: convertUtcToEpoch(SearchCriteria.criteria.dateFrom),
tEnd: convertUtcToEpoch(SearchCriteria.criteria.dateTo)
};
return params;
}
function volumeRequest() {
return CdnAnalyticsFactory.statsByDimension({
accountId: Token.UserInfo().Id
},
updateVolumeRequest())
.$promise;
}
Controller:
function getData() {
var data;
CdnService.sizeRequest(SearchCriteria.criteria.dateFrom, SearchCriteria.criteria.dateTo)
.then(function onSucess(response) {
data = CdnService.sumOfVolumeRequest(response)
});
}