Where to put the business rule in the angle?

5

I love programming web and mobile apps with angularjs. But I'm tired of spreading my business rules by controller and factorys. I miss being able to create my objects with their respective responsibilities, in addition to controllers services too, as I do in other languages.

How could I have an approach like this, oriented to objects with isolated responsibilities, in the angular? Has anyone implemented this?

    
asked by anonymous 03.06.2016 / 02:40

2 answers

4

If I understood your question well (and if my experience can answer well) I would say that you are not doing it wrong. In Angular, things get a little "scattered", the big question is to know how to "organize the mess".

Each of these (and there are many) elements have very specific purposes, which are often completely ignored. Whether for poorly crafted tutorials, for the convenience of writing everything in one place, generating less 'work' or even lack of knowledge.

  

Note: In Angular2 this already changes a lot, since it will be much simplified in this sense.

I, in particular, develop everything using well say only 3 components of the Angular:

  • Controller
  • Factory
  • Directive

Each has a very simple but very distinct purpose, see:

Factory : Responsible for getting data from a backend / api to feed the application with the required information.

Controller : Responsible for passing data (obtained by factory ) to the view and also for executing user-invoked functions through the UI.

Directive : Handling conditional HTML and / or creating common patterns functions in several views - Even though the views between them have no relation whatsoever.

In a simple scenario, imagine that you have an E-commerce. Once we access, we need to feed the site with the products. For this, the factory calls the backend and gets the data, storing it in a variable even within the factory itself.

The controller , in turn, will get this information in factory - Not caring how it was obtained, just interested to know that it exists, then passing it to the view via $scope , depending on the vm you use), so we feed the site with the products.

Assuming you want to add a product to the shopping cart, but knowing that the product can be accessed on both the home page and the "Bestseller" page on the "My list" page, categories, etc. Note which is the same function being called from different locations. Therefore, we can apply the function to a syntax , so it can be called independent of its directive , view or other component. The cart itself can be managed by a controller also, and it is accessed from any directive .

Although "messy", Angular has components with well-defined tasks and goals that can keep your application lean, unrepeatable, and in a way, well-organized. I believe it should be seen more as a whole than as isolated parts.

Complementary: A material that I have used ENOUGH and that has given me a good idea in organizing my code and my components, was this guide: link

    
03.06.2016 / 03:52
-2

Overview and Conceptual, official Angular

  

View Independent Business Logic: Runs on Services

     
    

As the application grows, it is a good practice to move the display-independent logic from the controller to a service, so that it can be reused by other parts of the application as well.

  

The advantage of working with Angular is component assembly, so you can change your business rule in the service and the front end continues to present the data without the need for adjustments in both the View and the controller.

    
14.03.2017 / 14:27