优雅的组织方式和用户ngResource

时间:2017-03-30 17:40:21

标签: angularjs code-organization

我的应用需要与各种表和视图进行交互。我是棱角分明的新人,我无法想出一种优雅的方式来组织我的ngResource应用程序中的服务。任何sugestions? 我认为我的控制器声明充满了服务。

/app/app.resource.js

angular.module('totalizacao').factory('ldap', ['$resource', function($resource) {
    return $resource('api.php/ldap/:usuario/:senha', {usuario: '@usuario', senha:''}, {
        login: {
            isArray: false,
            method: 'POST'
        }
    });
}]);


angular.module('totalizacao').factory('tb_agendamentos_llpp', ['$resource', function($resource) {
    return $resource('api.php/tb_agendamentos_llpp/:ID', {'ID' :''});
}]);

angular.module('totalizacao').factory('tb_atendimentos_llpp', ['$resource', function($resource) {
    return $resource('api.php/tb_atendimentos_llpp/:ID', {'ID' :''});
}]);

angular.module('totalizacao').factory('tb_grupo_oferta_llpp', ['$resource', function($resource) {
    return $resource('api.php/tb_grupo_oferta_llpp/', {});
}]);

angular.module('totalizacao').factory('tb_mvmt_llpp', ['$resource', function($resource) {
    return $resource('api.php/tb_mvmt_llpp/', {});
}]);

ngular.module('totalizacao').factory('tb_ofertas_llpp', ['$resource', function($resource) {
    return $resource('api.php/tb_ofertas_llpp/', {});
}]);


angular.module('totalizacao').factory('tb_score_llpp', ['$resource', function($resource) {
    return $resource('api.php/tb_score_llpp');
}]);

angular.module('totalizacao').factory('tb_vendas_llpp', ['$resource', function($resource) {
    return $resource('api.php/tb_vendas_llpp/:ID_LNHA', {ID_LNHA: ''});
}]);

angular.module('totalizacao').factory('tb_vendas_servicos_llpp', ['$resource', function($resource) {
    return $resource('api.php/tb_vendas_servicos_llpp/:ID', {'ID' :''});
}]);

angular.module('totalizacao').factory('vw_agendamentos_llpp', ['$resource', function($resource) {
    return $resource('api.php/vw_agendamentos_llpp/');
}]);

angular.module('totalizacao').factory('vw_arvore_ofertas_llpp', ['$resource', function($resource) {
    return $resource('api.php/vw_arvore_ofertas_llpp/', {});
}]);

.
.
.

我的控制器

angular.module('totalizacao').controller('arvoreOferta', 
            ['$routeParams','$rootScope','$scope','vw_vendas_llpp','vw_arvore_ofertas_llpp'
            ,'tb_grupo_oferta_llpp', 'tb_ofertas_llpp', 'tb_agendamentos_llpp','tb_atendimentos_llpp'
            ,'tb_vendas_servicos_llpp', 'vw_vendas_servicos_llpp'
             ,'vw_agendamentos_llpp','tb_score_llpp','vw_ofertas_totais','dateFilter'
        , function($routeParams, $rootScope, $scope, vw_vendas_llpp, vw_arvore_ofertas_llpp
                  ,tb_grupo_oferta_llpp, tb_ofertas_llpp, tb_agendamentos_llpp
                  , tb_atendimentos_llpp, tb_vendas_servicos_llpp, vw_vendas_servicos_llpp
                  , vw_agendamentos_llpp, tb_score_llpp, vw_ofertas_totais, dateFilter){

        var self = this;
.
.
.

1 个答案:

答案 0 :(得分:0)

我总是喜欢使用$ http,因为我可以有更多的控制使它成为通用的,因为$ resource内部使用$ http。但是因为你已经使用了$ resource我正努力以资源方式做到这一点。

请原谅我的语法和编译错误,因为我在这里直接输入。     你可以做的是创建一个公共服务或工厂说apiService或apiFactory然后传递所需的参数,http方法和动作等。     我不会进入编码细节,因为它是你如何概括和你传递的,但是这样的事情:

//First create a constant object:

angular.module('totalizacao').constant('apiConstants', {
    totalizacao: 'api.php/tb_mvmt_llpp/',
    totalizacao1: 'api.php/tb_mvmt_llpp1/'
});

// Generalise your factory as you want this is to give you an idea.
angular.module('totalizacao').factory('apiFactory', ['$resource','apiConstants', function($resource) {
    var apiFactory = {};
   /* Http Method  = GET, PUT, POST;
       MethodName = { 'get':    {method:'GET'},
    'save':   {method:'POST'},
    'query':  {method:'GET', isArray:true},
    'remove': {method:'DELETE'},
    'delete': {method:'DELETE'} };*/
    apiFactory.executeApi = function(url, methodName, httpMethod, params){
        var paramNames = Object.keys(params);// getKeys
        //construct url
        // you can put a for loop here to make it more generic for multiple params
        //params[paramNames[i]] will get you the value, here i just have one
        var completeUrl = url + ':' + paramNames[0]; // api.php/tb_mvmt_llpp/:paramName1

        var resources = $resource(completeUrl, null, {
                          apiMethod: {
                              method: httpMethod
                          }
                    });
       //you can return the promise or return the value
      resources[methodName](params) ///resources['get'](params)
            .$promise.then(function(user) {
          // do something

        });
        return // resources[methodName](params).$promise or some variable after

    };

    return apiFactory;
});

在你的控制器中,注入apiFactory,apiConstants并执行以下操作

    var params = {
       paramName1 : 'paramValue1'
    };
    apiFactory.executeApi(apiConstants.totalizacao, 'get', params).then(success,error);

    function success(){};
    function error(){};