Hi Sander,

 

Thanks for the reply. As you can probably tell, I am new to angularjs. I took a 
paid for video course on it, and used the app code in the course to "start 
off". After quite a few hours spent on creating my first app, I was clueless 
about how the http service worked with the server. The course did not use an 
actual server, but used hard coded arrays to return fixed data.  The following 
factory code is what was in the course, modified for http access to a server. 

 

 

app.factory('ActivitiesFactory', function($http) {

  var service = {};

                var service.entry;

  service.entries = [];

  

    service.getAll = function()  {

    $http.post('GetData.php', {command : 'getAll'})

        .success(function(data){

            service.entries =  data;

        })

        .error(function(data, status){

            alert('error!');

        });

}

    service.getById = function(id)  {

    $http.post('GetData.php', {command : 'getById'})

        .success(function(data){

            service.entry =  data;

        })

        .error(function(data, status){

            alert('error!');

        });

                }

  return service;

});

 

This sort of worked but when I called either function from the controller, the 
controller continued immediately after the call. It did not wait for the data 
to actually be returned from the server. This behavior was not apparent when I 
started writing in Angularjs. So, after spending much time on web searching on 
various Angularjs subjects, I found the code that I presented in the original 
post with the forum. I did some modification to the acutal http call. It does 
work and my controller gets control after the data is actually delivered by the 
server which is what I need for my app. So the bottom line here is that I need 
to be able to use the http service to obtain data from the server and be sure 
that my controller continues after the call only when the data has been 
actually delivered to the factory. I looked at your code and it is not apparent 
to me how it only returns when the data is "ready". As I said, I am a beginner 
with Angularjs so I am probably missing something.

Again, thanks for your help. I hope this explanation is precise enough. If not, 
I will gladly send more detail.

 

From: [email protected] [mailto:[email protected]] On Behalf Of 
Sander Elias
Sent: Tuesday, January 20, 2015 3:42 AM
To: [email protected]
Subject: [AngularJS] Re: how to use multiple functions for deferred http in a 
factory

 

Hi Jim,

Your code is needlessly complex, $http returns a promise to begin with.

    app.factory('getAllActivities', function ($http, $q) {
        return  {
            getAll: $http.post('GetData.php', {command : 'getAll'})
        }
    }
 
    app.factory('getAllActivities', function ($http, $q) {
        var all = $http.post('GetData.php', {command : 'getAll'})
        return  {
            getAll: all,
            getId: function (id) {}
               return $http.post('GetData.php', {command : 'getId',id:id})
            }
        return  {
        }
    }

The first service is exactly the same as your version, with the exception that 
you will retrieve the complete response, in stead of just the data (hence, you 
need to use result.data in your code)
the second one does only does 1 $http request and keeps that for reuse trough 
the entire life-cycle of your app, and the second method can fetch an id. (you 
probably want to do that every time…)

Does this help you enough? if you have additional questions, don’t hesitate to 
ask!

Regards
Sander

​

-- 
You received this message because you are subscribed to a topic in the Google 
Groups "AngularJS" group.
To unsubscribe from this topic, visit 
https://groups.google.com/d/topic/angular/6IlGxcU8BH8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to 
[email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to