Ravy
Ravy

Reputation: 4029

Why do we use Factories/Services for ajax calls in Angularjs?

I was taught that we use factories/services to eliminate the duplicate coding. Here's a part of the code which works fine.

app.controller('ServicesCtrl',['$scope','DataFactory',function($scope,$http,DataFactory){

    DataFactory.GetData('services1.json')
      .then(function(response){
         $scope.returnedData = response.data;
    })
      .catch(function(response){
        console.log('Error in process',response.status,response.data);
    });
  }]);

app.controller('ContactCtrl',['$scope','DataFactory', function($scope,DataFactory){

    DataFactory.GetData('location.json')
      .then(function(response){
         $scope.returnedData = response.data;
    })
      .catch(function(response){
        console.log('Error in process',response.status,response.data);
    });
  }]);

app.factory('DataFactory',['$http',function($http){

  var factory = {};

  factory.GetData = function(path) {
    return $http.get(path);

  }
  return factory;
}]);

My question is 1. Why use services/factories to make such ajax calls when we have to work on the promises inside controllers? I mean, I have to make the same .then and .catch calls in both the controllers here. Where is the efficiency in it? Is there any better way to do this? Or am I doing this wrong? Is it possible to to work on those promises inside the factories and return the response.data to different controllers?

Upvotes: 1

Views: 511

Answers (2)

Massimo Petrus
Massimo Petrus

Reputation: 1891

Hi we use factories and services

  • to make the application more modular,
  • to have the possibility to reuse the code,
  • to hide the implementation detail

For example a service which makes an http call it may be seen as an high level "service" which gives you back the required object, indipendently by the call type and it's reusable at high level.

The service allows to customize the call parameters, maybe avoiding that some of them are to be specified by the calling controller. Or it can do some preprocessing or post processing, it can do some caching and so on. And its portable, so you can call it each time you need.

For your last question

Is it possible to to work on those promises inside the factories and return the response.data to different controllers?

Maybe it's possible, but may be complex to implement as it has to do with the timing of the response. Instead i suggest you the $resource service in the module ngResource, which can already do what you neeed.

See docs:

AngularJs Tutorial on $resource

Angularjs Programming guide on $resource

Upvotes: 0

Abdullah Al Noman
Abdullah Al Noman

Reputation: 2878

The thing here is re-usability of code . Now suppose you have a service called

angular.module('test')
  .service('testService', function ($http) {

    this.getBidsUser = function ($username) {
      var endpoint = "bids/users/"+$username;

      return $http({
        method: 'get',
        url: endpoint
      });
    };

}

which returns bids of a user . You might want to use the same information in different views. So its a good idea to use service so that you dont have to rewrite same code again .

Once more you might want to have all the same end point related service on the same service for maintainability .

You might need to change end points for a service which will be hectic if you do not use service pattern .

Promises arr call backs . If you process those promises inside a service it will not be easy to maintain caller timeline .

Upvotes: 2

Related Questions