Source: directives/msi/msi.service.js

/**
 * @ngdoc directives
 * @name MsiService
 * @module s4c.directives.msi.MsiService
 *
 * @description
 * `MsiService` Componente para acesso ao api do backend e/ou comunicação entre controllers
 * 
 * 
 */
(function () {
    'use strict';

    function MsiService($http, $q, API_ENDPOINT) {
        this.incidenteDetalhamento = {
            incidente: {}
        };

        this.state = {
            isVisible: false
        };

        /**
         * @method fechar
         */
        function fechar() {
            this.incidenteDetalhamento.incidente = null;
            this.state.isVisible = false;
        }

        /**
         * @method getIncidentes
         */
        function getIncidentes() {
            var deferred = $q.defer();

            $http.get(API_ENDPOINT + 'incidentes/monitorados/rsoe')
                .then(function (res) {
                    if (typeof res.data === 'object') {
                        deferred.resolve(res.data);
                    } else {
                        deferred.reject(res);
                    }
                }, function () {
                    deferred.reject();
                });

            return deferred.promise;
        }

        var self = this;
        self.categorias = undefined;
        self.canLoad = true;
        self.deferred = $q.defer();

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

            if (self.categorias || !self.canLoad) {
                return self.deferred.promise;
            }

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

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

                        self.canLoad = true;
                        self.categorias = response.data;
                        self.deferred.resolve(response.data);
                    } else {
                        self.deferred.reject(response);
                    }
                }, function () {
                    self.deferred.reject();
                });

            return self.deferred.promise;
        }

        return {
            getIncidentes: getIncidentes,
            getCategorias: getCategorias,
            fechar: fechar,
            incidenteDetalhamento: this.incidenteDetalhamento,
            state: this.state
        };

    }

    MsiService.$inject = [
        '$http',
        '$q',
        'API_ENDPOINT'
    ];

    angular.module('s4c.components.msi')
        .service('MsiService', MsiService);

})();