18

I'm following the Tutorial from the official AngularJS docs and I want to know if I can add another function to the Phone factory so that I can organize code better. They have declared a "query" function, but what if I wanted to add a query2 function that references a different url...say phones2/:phoneName.json for example?

Factory declaration:

var phonecatServices = angular.module('phonecatServices', ['ngResource']);

phonecatServices.factory('Phone', ['$resource',
  function($resource){
    return $resource('phones/:phoneId.json', {}, {
      query: {method:'GET', params:{phoneId:'phones'}, isArray:true}
    });
  }]);

I have tried a number of things and non of them seem to be working :s

This answer seems to be on the right track, but the syntax for each factory function doesn't quite match up with the above factory.

Something along the lines of:

phonecatServices.factory('Phone', ['$resource',
      function($resource){
       return {
        query: ...
        query2: ...
       }
      }]);
Community
  • 1
  • 1
SomethingOn
  • 9,813
  • 17
  • 68
  • 107
  • Can you provide an example of what you are trying? – Chandermani Feb 17 '14 at 05:10
  • you could provide `url` in `query2` method as : `query2 : {method:'GET', url : '/phones2/:phoneName.json', params:{phoneName:'ph1'}, isArray:true}`. But I think there is more better way of doing it. This approach appends unwanted `?` in the `URL`. You can give a look and comment on that. – Lekhnath Feb 17 '14 at 05:15
  • The accepted answer here worked: http://stackoverflow.com/questions/17160771/angularjs-a-service-that-serves-multiple-resource-urls-data-sources. – SomethingOn Feb 17 '14 at 06:42

3 Answers3

26

One such example of this is: Link for Demo

angular.module('services', []).factory('factoryName', ["$filter",
  function($filter) {
    var method1Logic = function(args) {
      //code
    };
    var method2Logic = function(args) {
     //code
    };
    return {
      method1: method1Logic,
      method2: method1Logic
    };
  }
]).controller('MainController', ["$scope", "$rootScope", "$filter", "factoryName", function ($scope, $rootScope, $filter,factoryName) {
     $scope.testMethod1 = function(arg){
       $scope.val1 = factoryName.method1(arg);
     };

     $scope.testMethod2 = function(arg){
       $scope.val2 = factoryName.method2(arg);
     };
}]);

There is even a better version Opinionated version of this: References

function AnotherService () {

  var AnotherService = {};

  AnotherService.someValue = '';

  AnotherService.someMethod = function () {

  };

  return AnotherService;
}
angular
  .module('app')
  .factory('AnotherService', AnotherService);
Community
  • 1
  • 1
Abhijeet
  • 8,561
  • 5
  • 70
  • 76
  • Thanks Abhijeet, I like you method ;) – SomethingOn Sep 09 '15 at 15:33
  • 1
    This method is much more readable and therefore I changed it to the accepted answer – SomethingOn Oct 09 '15 at 03:35
  • awesome! very useful – Hamzeen Hameem Jun 29 '16 at 03:54
  • @Abhijeet - could you, or someone, demonstrate how to `sort` the results of `method1Logic` (assuming there is $http content waiting to return) so that the results are sorted BEFORE the results are returned to the controller....or how to sort the results the instance they are returned to the controller. Having a hard time figuring that out. – rolinger Feb 01 '18 at 15:14
14

This is the service code:

myServices.factory('Auth', ['$resource',
  function($resource){
    return {
      Login: $resource(serviceURL + 'login', {}, { go: { method:'POST', isArray: false }}),
      Logout: $resource(serviceURL + 'logout', {}, { go: { method:'POST', isArray: false }}),
      Register: $resource(serviceURL + 'register', {}, { go: { method:'POST', isArray: false }}),
    };
  }
]);

And from my controller I just have to add the go() function call to make it work:

Auth.Login.go({ username: $scope.username, password: $scope.password },

I guess I could have named the go function after the method and called it "post()" instead for clarity...

SomethingOn
  • 9,813
  • 17
  • 68
  • 107
0

Yes, Of course, you can have multiple functions in an object. The only caveat is your service should return an object. You can have all the valid javascript members in that object as long as you follow object's syntax.

So following is possible

phonecatServices.factory('Phone', ['$resource',
  function($resource){
      return {
            query: ... , // NOTICE THE COMMA HERE 
            query2: ...
       }
}]);

You must be missing the comma (,) to separate your object's key values.

Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61