Mystery for me ... I have my controller and my services. Why is the service LocalDataService.getCacheUserUpdate(); not launched in it ? Here is some code details. I modified the code to follow the same pattern as the chatsUpdate but the LocalDataService.getCacheUserUpdate(); gets skipped all the time when I debug step by step... but it is triggered after the function pull refresh is complete
Thanks!
My controller:
// Check for new updates in conversations and return the number of new messages
var getChatsUpdates = function() {
DataService.getHasNewMessages().success(function(response) {
$scope.chatsUpdates = response.chats_updates;
//console.log($scope.chatsUpdates)
});
};
var updateUser = function() {
LocalDataService.getCacheUserUpdate().success(function(response) {
$scope.user = [];
$scope.user = DataService.getUser();
}).error(function() {
console.log('error on userupdate')
})
}
var updateUserFreeStatus = function() {
updateUser()
$scope.user_free_status = [];
$scope.user_free_status = DataService.getUserFreeStatus();
}
$scope.doHomeRefresh = function() {
console.log('Start refreshing!')
updateUserFreeStatus()
getChatsUpdates()
$scope.$broadcast('scroll.refreshComplete');
$scope.$apply()
console.log('refreshed!')
};
And my service:
.factory('LocalDataService', function(WebService, $localstorage) {
return {
getCacheUserUpdate: function() {
var userUpdate = WebService.get('profile/get/user/update');
userUpdate.success(function(response) {
$localstorage.unset('user')
console.log(response.user)
$localstorage.setObject('user', response.user)
});
return userUpdate;
}
};
})
.factory('DataService', function($q, $localstorage, WebService) {
return {
getUser: function() {
return $localstorage.getObject('user');
},
// get updates to check if new messages in conversations
getHasNewMessages: function() {
var updates = WebService.get('conversations/updates/new-messages');
updates.success(function(response) {
// nothing
});
updates.error(function(response) {
console.log('error in get messages: ' + response)
});
return updates;
}
};
})
I really don't get what is happening there...
As Emanual Ralha said, your webservice needs to return promise objects. Basically speaking:
var foo = $http.get('http://localhost/potatoes/1')
doesn't actually put the results of the HTTP call into foo, because that will take a while to finish up, and that's not how javascript actually works.
Instead, foo is a promise to a value- you need to do
$http.get().then(function(data) {
$scope.potato = data.data;
});
In that situation, the callback function gets invoked when the $http is done (it is synonymous with .success, but .success is nonstandard, so get used to .then and .catch), and you can deal with the results at that point.