I'm using a deferred promise because I need the app to wait for the result of an asynchronous call. The function retrieves the correct data, but I can't seem to get that back to my controller.
Specifically, the line of code
data : multiBarChartData
is not getting the resolved data from
function multiBarChartData()
Here is the factory:
angular.module('hapDash.services', []).factory('DataService', function ($http, $q, SESSION_ID) {
return {
multiBarChart : {
data : multiBarChartData
}
};
function multiBarChartData() {
var deferred = $q.defer();
$http({
method : 'GET',
url : '/services/data/v37.0/analytics/reports/00OP0000000Q9iB',
headers : {
'Authorization' : 'Bearer ' + SESSION_ID
}
}).success(function (data) {
var report02ChartData = [];
angular.forEach(data.groupingsDown.groupings, function (de, di) {
var report02Values = [];
report02ChartData.push({'key' : de.label, 'values' : report02Values});
angular.forEach(data.groupingsAcross.groupings, function (ae, ai) {
report02Values.push({'x' : ae.label, 'y' : data.factMap[de.key + '!' + ae.key].aggregates[0].value});
});
});
data = report02ChartData;
deferred.resolve(data);
}).error(function () {
deferred.reject('There was an error accessing the Analytics API')
})
return deferred.promise;
}
});
... and here is the controller:
var app = angular.module('hapDash', ['config', 'nvd3', 'gridster', 'hapDash.services']);
app.controller('HapDashCtrl', function ($scope, $timeout, DataService) {
$scope.dashboard = {
widgets : [{
col : 0,
row : 0,
sizeY : 1,
sizeX : 1,
name : "Multi Bar Chart",
chart : {
data : DataService.multiBarChart.data(),
api : {}
}
}
]
};
});
I'm trying to get the controller code
data : DataService.multiBarChart.data(),
to pull the async response once it is complete.
What am I doing wrong?
$http()itself returns a promise, there is no need to manually create another one.