Answer the question
In order to leave comments, you need to log in
How to avoid creating a large number of services in an angularjs project?
Good day to all.
There is a project in which 20 api which of course give different data depending on the sent data url.
--- https:example.com?fileds=name,id
--- https:example.com?fileds=name,id,booking,staff,users
What is the right way to organize a project?
1) Make one folder where all Models will be stored and these models may be more than 20, which means we create 20 singletons in the global space, which will be available anywhere after injection in the controller
function(bookings, users){
$scope.bookings = bookings.getAll();
$scope.users = users.getAll();
here you can also do UPDATE, DELETE etc.
}
2) Make one Service http.js in which the keys https: api? keys will be configured and call it in the controller at the right time - this means that we do not create services as such to receive data, but simply contact the http.js service to receive data , i.e.
function(http){
http.getAll('bookings,name,user').then(/RESPONSE/);
http.getAll('users,name,is,picture').then(\RESPONSE\)
here you can also do UPDATE, DELETE etc.
}
Dear experts, which of the options do you consider more convenient for raising the project? and why I will wait for your answer.
Answer the question
In order to leave comments, you need to log in
Break the project into modules (depending on the semantics of the entities), let each module have its own folder with services. So it will be more order and more convenient to work. Create one service per relationship with one entity.
Or put each component in a separate folder, and the service it uses is already in it, but in this approach there is a possibility of duplication of services (accidentally)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question