Controller and services logic with Angularjs

2

I have some doubts about the logic of the search functions of the controller and the service.

Here is an example of service :

angular.module("myApp").factory("ProjetosAPI", function ($http, config) {

    var _getProjetos = function (pagina, total) {
        return $http.get(config.baseUrl + "/projetos?pagina=" + pagina + "&total=" + total);
    };

    var _getProjetosPorEmpresa = function (pagina, total, empresa) {
        return $http.get(config.baseUrl + "/projetos?pagina=" + pagina + "&total=" + total + "&empresa=" + empresa);
    };

    var _getProjetosPorEmpresaData = function (pagina, total, empresa, data) {
        return $http.get(config.baseUrl + "/projetos?pagina=" + pagina + "&total=" + total + "&empresa=" + empresa + "&data=" + data);
    };

    return {
        getProjetos: _getProjetos,
        getProjetosPorEmpresa: _getProjetosPoEmpresa,
        getProjetosPorEmpresaData: _getProjetosPoEmpresaData
    };

});

Questions:

1- Is this approach "right"?

2- If I needed to search by company, date and value or just by date, would I add another function? For example: getProjetosPorEmpresaDataValor or getProjetoPorData .

In the controller , would I need to have a function for each function of the service , with the appropriate parameters?

I'm using it this way so far. In the REST API (node.js + express.js) I have a single function for GET /projetos which handles which data to bring according to the parameters received in the request.

As I have never developed a great application, I have many similar doubts, if you can tell me some material that would help in that sense, it would be good.

    
asked by anonymous 08.12.2016 / 19:29

2 answers

3

There are several ways to write your services. There is nothing essentially wrong with your approach.

There are standards, however, that are very popular, such as John's Style Guide Pope. Having said that, it is worth mentioning that the way you have structured your service is the model not recommended by most of the community. The reasons for this are all listed in the services section.

The Style Guide basically says that it is better to use function declarations to hide implementation details, that is, to keep the accessible members of the factory at the very top of the document so that you can see, when you open the file, which are the functions of that factory and which functions can be accessed externally by other controllers. This makes it much easier when your service starts getting very long in large, complex applications.

To optimize your service, I would still advise you to modify the parameters of your _getProjects function

var _getProjetos = function (query) {
    return $http.get(config.baseUrl + query);
};

By doing this, you get a much more reusable function in your factory. To achieve the same results, you have to mount the queries in your controller according to the requirements of each call and only pass the final pro service string, which will execute the GET.

Style Guide Examples

/* avoid */
function dataService() {

  var someValue = '';

  function save() {
    /* */
  };

  function validate() {
   /* */
  };

  return {
    save: save,
    someValue: someValue,
    validate: validate
  };

}

/* recommended */
angular
   .module('app.core')
   .factory('dataservice', dataservice);

function dataService() {

  var someValue = '';

  var service = {
    save: save,
    someValue: someValue,
    validate: validate
  };

  return service;

  ////////////

  function save() {
      /* */
  };

  function validate() {
      /* */
  };
}
    
03.01.2017 / 16:09
2

This is one more question regarding code review than rather than using services properly.

  

1- Is this approach "right"?

Nothing invalid with it. It can, however, be enhanced, for example with the addition of individual caching or promises , according to project needs.

  

2- If I needed to search by company, date and value or just by date, would I add another function? For example: getProjetosPorEmpresaDataValor or getProjetoPorData ?

You are using static interfaces for your methods. If the presence of parameters is variable, you can take advantage of abstract interfaces to avoid creating an unnecessary number of functions corresponding to the parameter combinations.

For example, instead of:

var _getProjetos = function (pagina, total) {
    return $http.get(config.baseUrl + "/projetos?pagina=" + pagina + "&total=" + total);
};

You can implement the following abstract object interface:

var _getProjetos = function (params) {
    return $http.get(config.baseUrl + "/projetos?" + $.param(params);
};

$.param() converts an object to the notation used in query strings . Your call to service can be made like this, then:

var ret = svc.getProjetos({pagina: 1, total: 10});
// URL gerada: config.baseUrl + "/projetos?pagina=1&total=10";

If you want to add a business code:

var ret = svc.getProjetos({pagina: 1, total: 10, empresa: 128 });
// URL gerada: config.baseUrl + "/projetos?pagina=1&total=10&empresa=128";
  

3- In the controller would I need to have a function for each function of the service, with the appropriate parameters?

No. This will depend solely on your implementation model.

    
03.01.2017 / 17:16