2

I have a factory that serves up three different $http.get methods.

angular.module('myApp')
 .factory('getFactory', function ($http, $q) {

 return {

  methodOne: function () {
   var deferred  = $q.defer();

   $http.get('path/to/data')
    .then(function successCallback (data) {
         deferred.resolve(data);
        },function errorCallback (response) {
                                        console.log(response);
                                });

   return deferred.promise;
  },

  methodTwo: function (arg1, arg2) {
   var deferred  = $q.defer();

   $http.get('path/to/' + arg1 + '/some/' + arg2 + 'more/data')
    .then(function successCallback (data) {
         deferred.resolve(data);
        },function errorCallback (response) {
                                        console.log(response);
                                });

   return deferred.promise;
  },

  methodThree: function (arg1, arg2, arg3) {
   var deferred  = $q.defer();

   $http.get('path/to/' + arg1 + '/some/' + arg2 + '/more/' + arg3 + '/data')
    .then(function successCallback (data) {
         deferred.resolve(data);
        },function errorCallback (response) {
                                        console.log(response);
                                });

   return deferred.promise;
  },
 };
 });

Basically, these methods only differ in the path it gets the data from. The data these methods get are handled in the controller. I have been reading a lot of Angular best practices on the side and have been seeing DRY (Don't Repeat Yourself) tips.

I feel the code I have above is too repetitive. Is there a better way of coding this the Angular way?

**Note: I used the yeoman generator to scaffold the directories of my project.

TylerH
  • 20,799
  • 66
  • 75
  • 101
floatyourboat
  • 83
  • 2
  • 9
  • 1
    AFAIK `success` and `error` are deprecated, and you should use `then` insetad. – Alexander Bondar Oct 28 '15 at 14:18
  • @AlexanderBondar Thank you for that notice. I just read up on it now on the [AngularJS $http documentation](https://docs.angularjs.org/api/ng/service/$http). Will edit code. – floatyourboat Oct 28 '15 at 14:24

1 Answers1

4
angular.module('myApp')
    .factory('getFactory', function ($http, $q) {

    //Using revealing Module pattern
    var exposedAPI = {
        methodOne: methodOne,
        methodTwo: methodTwo,
        methodThree: methodThree
    };

    return exposedAPI;

    //Private function, not required to be exposed
    function get(url){
        //$http itself returns a promise, so no need to explicitly create another deferred object
        return $http.get(url)
                .then(function (data) {
                    //Create deferred object only if you want to manipulate returned data
                }, function (msg, code) {                       
                    console.log(msg, code);
                });
    }

    function methodOne() {
        //DRY
        return get('path/to/data');
    }

    function methodTwo(arg1, arg2) {
        return get('path/to/' + arg1 + '/some/' + arg2 + 'more/data');
    }

    function methodThree(arg1, arg2, arg3) {
        return get('path/to/' + arg1 + '/some/' + arg2 + '/more/' + arg3 + '/data');
    }
    });
Kulbhushan Singh
  • 536
  • 5
  • 18