3

I have this service :

(function() {
    'use strict';
    angular
        .module('myApp')
        .factory('Consultant', Consultant);

    Consultant.$inject = ['$resource', 'DateUtils'];

    function Consultant ($resource, DateUtils) {
        var resourceUrl =  'api/consultants/:id';

        return $resource(resourceUrl, {}, {
            'query': { method: 'GET', isArray: true},
            'get': {
                method: 'GET',
                transformResponse: function (data) {
                    data = angular.fromJson(data);
                    return data;
                }
            },
            'update': {
                method: 'PUT',
                transformRequest: function (data) {
                    return angular.toJson(data);
                },
                transformResponse: function (data) {
                    data = angular.fromJson(data);
                    return data;
                }
            },
            'save': {
                method: 'POST',
                transformRequest: function (data) {
                    return angular.toJson(data);
                },
                transformResponse: function (data) {
                    data = angular.fromJson(data);
                    return data;
                }
            }
        });
    }
})();

What if I want to add the url api/consultantscustom/:id (with its query/get/update methods) to this service?
As it seems that this file can contain only one factory, is there a way to do that or do I need to create another file with a new factory?

1 Answer 1

2

Maybe I am totally misunderstanding what you are asking, but you can have as many factories in one file as you like (or as seems useful):

(function() {
    'use strict';

    angular.module('myApp')
      .factory('Consultant', Consultant)
      .factory('Custom', Custom);

    Consultant.$inject = ['$resource', 'DateUtils'];
    Custom.$inject = ['$resource'];

    function Consultant ($resource, DateUtils) {
        var resourceUrl =  'api/consultants/:id';
        ...
    }

    function Custom ($resource) {
        var resourceUrl =  'api/consultantscustom/:id';
        ...
    }

Or, if you want to re-use the same factory to access different URLs, you could add methods to set the URL and then re-use the call to the generic resource.

function Consultant ($resource, DateUtils) {

  function consultants () {
    _call_resource('api/consultants/:id');
  }
  function custom () {
    _call_resource('api/consultantscustom/:id');
  }

  function _call_resource (resourceUrl) {
    return $resource(resourceUrl, {}, {
      ...
    }
  }

  this.consultants = consultants;
  this.custom = custom;
  return this;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.