I've been writing a somewhat complicated service that involves a lot of API function calls in Angular. After a somewhat tortured first attempt I have tried to take more of an MVC approach rewritten the Angular in the following ways.
All data, once it's retrieved from the API, is stored in a Service with a setter or getter, i. e. (MODEL):
.service('userFirstName', function() { var userFirstNameProp; return { getter: function() { return userFirstNameProp; }, setter: function(value) { userFirstNameProp = value; } }; })
That way the data can be retrived from any controller via the setter or getter with a simple function call to determine whether it's already been properly set.
All API calls are pushed into factories using Angular's $resource module. A sample call might be (CONTROLLER):
.factory("someQueryFactory", function($resource, userToken) { var resource = $resource(serverPath + apiPath, {}, { get: { method: "GET", headers: { 'uid': function() { var uid= curUid.getter(); return token; } } } }); return resource; })
Each "page" is given its own route, partial, and controller (VIEW):
$routeProvider .when('/', { templateUrl: '/partials/login1.html', controller: 'homePageController' })
Each controller handles the logic of each page. I avoided using directives because the logic of the pages is not too complex and almost entirely consists of seeing if the user's last step has been completed to the satisfaction of the user's next step (i. e. has the user logged in? Then take them to the home page.)
So far so good, but two issues:
I notice my controller headers seem fairly bloated, mostly because of the need to include the the modules, services, and factories in them. So maybe something like:
LSPortal.controller('portalLoginController', ['$scope', '$log', '$location', 'thingOne', 'thingTwo', 'thingThree', 'userStatus', 'userFirstName', 'userLastName', 'userPetName', 'userColor', 'userGender', 'apiQueryOne', 'apiQueryTwo', 'apiQueryThree', function($scope, $log, $location, thingOne, thingTwo, thingThree, userStatus, userFirstName, userLastName, userPetName, userColor, userGender, apiQueryOne, apiQueryTwo, apiQueryThree) { //logic }]);
I understand I could use something like RestMod or Restangular to reduce multiple query calls, but the API is complex and I want something that's going to be supported going forward in Angular that I don't have to throw out in three months.
It's also a lot of getter and setter calls, many times involving doing a bunch of setting at the end of one page only to have to do a lot of getting at the beginning of the next. This seems like an improvement over my formerly somewhat tortured use of $scope, but is it?
I've been feeling free to mix AngularJS with regular JS but the more I read the more it seems I should try to keep things in Angular as much as possible. Not sure if this is illustrated above but if someone needs a better example I'd be glad to provide one.
And is there anything else I'm missing? A lot of tutorials go over certain facets of angular but very few seek to put everything together. All help appreciated.