angular project structure, Services & Controllers

sch

Earlier today I asked about how to call one controller from another, I was having a lot of trouble with what functions should be where etc. This got me thinking on the bigger picture, on how I should build my project, to be as clean and logical as possible.

My application works against a SharePoint server, I have service.js files with $resource factorys to communicate with SharePoint, an example:

ItemService.js

myApp.service('ItemService', function ($q, Item) {

    this.getItem = function (id){
        var deferred = $q.defer();
        setTimeout(function() {
            var item = Item.get({ID: id }, function(){
                deferred.resolve(item.d.results[0]);
            }, function(error){
                deferred.reject(error);
            });
        }, 1000);
        return deferred.promise;
    };

});

myApp.factory('Item', function($resource) {
    return $resource("serverURL/lists/getByTitle('Items')/items", {}, {
        get: {
            method: 'GET',
            headers: { "Accept": "application/json; odata=verbose" },
            url: "serverURL/lists/getByTitle('Items')/items?$select=&$filter=ID eq :ID"
        }
    });
});

To use these services I have a controller file, example:

ItemController.js

angular.module('myApp').controller('ItemController', function ($scope, $modal, $log, ItemService) {

    $scope.loadItem = function(id){
        var promise = ItemService.getItem(id);
        promise.then(function(item){
            $scope.viewItem(item);
        }, function(err){console.log(err);})
    };

});

This was example of the controller and service files for the sharepoint list Items, I also have one controller and one service for Tasks, as well as the main controller, AppController.

Are my extra controllers unnecessary? Should I just inject my services into my AppController, and write my function there instead?

My problem is that when my project gets bigger, this way is easier to read and understand, but code gets more complicated since controllers would have to communicate a lot. On the other hand, if I inject my Services into my AppController it would be hard to read, but still work as intended.

mgiesa

I like to keep my controllers responsible for a single thing.

For example, I have a project management app. On the project page there is a search on the left side and the selected project on the right. I have a controller for each because they are unrelated enough that I can change one without affecting the other.

The selected project display has three tabs, one for general info, one for a list of tasks and another for a list of contacts. I have a controller for each tab. Because they all inject the project service they all have access to the same project object when they instantiate, and this keeps each controller focused.

When the user adds a task or a contact I display a modal with a list of available tasks or contacts. A separate controller handles that list and modal.

It's the single responsibility principle and it works well to keep your code clean, even if it means having a few more controllers. It also means my stuff is reusable. If I want to have one of those modals on a different entity I can, because the modal has no concept of the project, it only knows it's doing a search for tasks or projects and returning the selected ones to its caller. If I had one big controller for everything I would have to write that code again.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

No "Project settings" in "Project structure"

From Dev

How should angular services convey exceptions to controllers?

From Dev

Resolving ServiceStack Services in MVC Controllers

From Dev

Proper way to initialize angular services using the controllers

From Dev

Angular.js sanity check: Services vs. Factories vs. Controllers... + Directives + Behavior

From Dev

How to structure controllers and services

From Dev

Handling Errors From Services in Controllers

From Dev

Angular JS Services and Controllers in Different Files Causes Error

From Dev

Adding Services to Controllers in AngularJS

From Dev

Controlling order of operations with services and controllers

From Dev

Communication between controllers in Angular

From Dev

How to structure creating/updating data in Angular project with Firebase

From Dev

Visual Studio Team Services/TFS 2015 Project Structure

From Dev

Angular project structure (folders, routes, services, etc)?

From Dev

Cloud service not available in the Developer services of android studio 2.2 preview 2 project structure

From Dev

In my Angular JS application, I want to get know which services, modules are used in controllers?

From Dev

How to structure Node/Angular/Socket.io project?

From Dev

Best way to structure controllers, services code on AngularJS

From Dev

Testing controllers with services

From Dev

How to structure controllers using swift?

From Dev

Separating Angular Routing, Controllers, Services

From Dev

Laravel Controllers Structure And Numbers of Controller

From Dev

Cloud service not available in the Developer services of android studio 2.2 preview 2 project structure

From Dev

Should I have more angular services or controllers?

From Dev

What are Repositories, Services, and Actions/Controllers?

From Dev

google-services.json integration for non-standart android project structure

From Dev

Java Spring project structure - controllers aren't detected

From Dev

Grouping Angular services together to inject them in multiple Controllers

From Dev

ASP.NET Boilerplate project structure for new entities/services

Related Related

  1. 1

    No "Project settings" in "Project structure"

  2. 2

    How should angular services convey exceptions to controllers?

  3. 3

    Resolving ServiceStack Services in MVC Controllers

  4. 4

    Proper way to initialize angular services using the controllers

  5. 5

    Angular.js sanity check: Services vs. Factories vs. Controllers... + Directives + Behavior

  6. 6

    How to structure controllers and services

  7. 7

    Handling Errors From Services in Controllers

  8. 8

    Angular JS Services and Controllers in Different Files Causes Error

  9. 9

    Adding Services to Controllers in AngularJS

  10. 10

    Controlling order of operations with services and controllers

  11. 11

    Communication between controllers in Angular

  12. 12

    How to structure creating/updating data in Angular project with Firebase

  13. 13

    Visual Studio Team Services/TFS 2015 Project Structure

  14. 14

    Angular project structure (folders, routes, services, etc)?

  15. 15

    Cloud service not available in the Developer services of android studio 2.2 preview 2 project structure

  16. 16

    In my Angular JS application, I want to get know which services, modules are used in controllers?

  17. 17

    How to structure Node/Angular/Socket.io project?

  18. 18

    Best way to structure controllers, services code on AngularJS

  19. 19

    Testing controllers with services

  20. 20

    How to structure controllers using swift?

  21. 21

    Separating Angular Routing, Controllers, Services

  22. 22

    Laravel Controllers Structure And Numbers of Controller

  23. 23

    Cloud service not available in the Developer services of android studio 2.2 preview 2 project structure

  24. 24

    Should I have more angular services or controllers?

  25. 25

    What are Repositories, Services, and Actions/Controllers?

  26. 26

    google-services.json integration for non-standart android project structure

  27. 27

    Java Spring project structure - controllers aren't detected

  28. 28

    Grouping Angular services together to inject them in multiple Controllers

  29. 29

    ASP.NET Boilerplate project structure for new entities/services

HotTag

Archive