Source: services/Departamento.js

    /**
     * @ngdoc service
     * @name Departamento
     * @module s4c.services.Departamento
     * 
     * @description  Componente para acesso a api do backend e/ou comunicação entre controllers
     * 
     *
     */
     
'use strict';

(function () {
	
   /**	
	* @method Departamento
	* @param {*} $http
	* @param {*} $q
	* @param {*} API_ENDPOINT
	* @param {*} $resource
	*/
    function Departamento($resource, API_ENDPOINT, $q, $http) {

  		/**	
		* @method crud
		*/
        function crud() {
            return $resource(API_ENDPOINT + 'departamentos/:id',
                { id: '@id' },
                {
                    save: { method: 'PUT' },
                    delete: { method: 'DELETE' }
                });
        }

        var self = this;
        self.agencias = undefined;
        self.canLoad = true;
        self.p = $q.defer();

  		/**	
		* @method getAgencias
		*/
        function getAgencias() {

            if (self.agencias || !self.canLoad) {
                return self.p.promise;
            }

            self.canLoad = false;
            $http.get(API_ENDPOINT + 'departamentos')
                .then(function (response) {

                    if (typeof response.data === 'object') {

                        self.canLoad = true;
                        self.agencias = response.data;
                        self.p.resolve(response.data);

                    } else {
                        self.p.reject(response);
                    }
                }, function (err) {
                    self.p.reject(err);
                });

            return self.p.promise;
        }

        return {
            crud: crud,
            getAgencias: getAgencias
        };
    }

    angular.module('s4c.services.Departamento', [])
        .factory('Departamento', Departamento);

    Departamento.$inject = ['$resource', 'API_ENDPOINT', '$q', '$http'];
}());