I'm having trouble returning data from an Angular promise in a factory back to the controller. In this example,
update: function (staffList) {
// get local last updated timestamp
var lastUpdatedLocal = "";
var lastUpdatedRemote = "";
for (i=0; i < staffList.length; i++) {
// get largest (most recent) timestamp
if(staffList[i].entry_date > lastUpdatedLocal) {
lastUpdatedLocal = staffList[i].entry_date;
}
}
// get remote last updated timestamp
var promise = $http.get('REMOTE_API_CALL')
.success(function(data, status, header, config) {
return lastUpdatedRemote = data[0].entry_date;
}).then(function(response) {
return response.data[0].entry_date;
});
console.log(promise);
}
I want to filter local data to get a local timestamp, compare it against a remote timestamp and instruct the controller to re-download all data if the local timestamp is less than the remote timestamp.
I'm filtering the local data okay and getting the expected result. Where I seem to get stuck is with promises. I can log out the filtered remote data within the .success or .then methods but haven't had much luck returning the values out of the promises. I know there's probably an issue with variable scope here.
Also, am I best to return a boolean from my factory to my controller (should I update? true/false) or perhaps a small obj with the local & remote timestamped data and leave the controller decide what to do from there?
Thanks in advance.