Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70429

Best Approach to put networking into a service?

$
0
0

@ajbeckner wrote:

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:

1) 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;
};

2) Changing the return (to avoid returning 'stuff = { }'):

someQueryFunc = function( ) {
    $http.get(blahblahblah).then(function successCallback(response) {
        return response.data
    }, function errorCallback(response) {
        //handles error
    });
};

3) 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.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70429

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>