So I am new to ionic and have an app I am working on. It has a bunch of views and a bunch of http.get calls. I decided I want to move all of the http get calls out of my controller files and into a nice service called ‘Queries’, but I can’t seem to figure out a viable approach. I believe the problem has to do with $http being asynchronous. Here is what I have tried:
-
moving my code verbatim to the service:
someQueryFunc = function( ) {
var stuff = {}
$http.get(blahblahblah).then(function successCallback(response) {
stuff = response.data
}, function errorCallback(response) {
//handles error
});
return stuff;
}; -
Changing the return (to avoid returning ‘stuff = { }’):
someQueryFunc = function( ) {
$http.get(blahblahblah).then(function successCallback(response) {
return response.data
}, function errorCallback(response) {
//handles error
});
}; -
Passing the object from $scope and hoping pass-by-reference solves my problem:
someQueryFunc = function(objPassedInFromScope) {
$http.get(blahblahblah).then(function successCallback(response) {
objPassedInFromScope = response.data
}, function errorCallback(response) {
//handles error
});
};
but these are all no goes. Any ideas? I imagine that something like this must be a regular design pattern that i just haven’t seen in ionic yet, pointers to good examples appreciated.