Live data from Hacker News

Angular-tips: Consuming services

angular-tips.com

11–14 of 14 posts

Re: Angular-tips: Consuming services

#11
Hello friends, I updated the post with your new insights. I hope you find it better. If not, please reply all you need or even better (Make some pull requests :P).

As I said in the first post, I am the first who is learning Angular.js but I find that teaching what are you learning is the best way to learn. So here am I teaching the best I can.

Of course, there is things that I ignore, so I really appreciate all your comments, so I learnt a LOT about services today.

Thank you.

Re: Angular-tips: Consuming services

#12
post #10
post #5

1. This tightly couples the service to the view. Changes in the view and/or service will potentially result in nasty surprises and spaghetti code. For instance, what happens if the business processes change one day and "login" needs to return "logged in but email address unconfirmed"? You want the rules to deal with that scenario confined to a LoginController, not scattered around through each view (consider: an omni…

Is it not ok to have all of that state you're talking about down in the service? In my experience any state you have in the controllers can be problematic in bigger apps. When I started out I used to proxy everything through the controller but I've found that you end up with a lot of duplication that doesn't ever seem to pay off. When you're talking about the various auth states you might have - they could be changed…

I'm mostly a back-end developer and I don't have much experience with rich front-end interfaces other than my own little side projects and a WPF project from about a year back - however, I do believe the same principles apply to both front- and back-end design.

Specifically, you should be following the SRP - duplicated code is a symptom that you are not. Without seeing your code that you feel doesn't pay off, it's difficult to answer your question more directly.

I will say there's no reason you can't design your server-side services to be consumed directly by the view, in which case I wouldn't see a problem referencing the service directly from the view (e.g. the UserService exposed by the server implements (a) normal login, (b) login as part of checkout and (c) registration).

Alternatively, you could build a client-side service to wrap the server-side service and reference that directly from your views (e.g. a UserController that wraps the UserService and is consumed by (a) the omnipresent login view, (b) a login/signup view, and (c) the registration view).

Over and above this, there is some middle-ground where you reuse structures. For example, a User structure (with IsAuthenticated, UserName, FullName etc.) would be assigned to a $scope.CurrentUser property for the views. Consequently you don't reimplement the User object the service implements, and you don't vaguely depend on the entire UserService, but the data contract the view does depend on from the controller is still explicit by looking at the $scope definition in the controller. Similarly, the operation contract is defined by the controller functions. This improves maintainability and readability IMO.

Re: Angular-tips: Consuming services

#13
post #5

1. This tightly couples the service to the view. Changes in the view and/or service will potentially result in nasty surprises and spaghetti code. For instance, what happens if the business processes change one day and "login" needs to return "logged in but email address unconfirmed"? You want the rules to deal with that scenario confined to a LoginController, not scattered around through each view (consider: an omni…

It's a problem I have in Angular since I've started using it. You put your data/model in a service, but you need some GUI elements to display the various states of these data, so you put html in the view, some helpers in the controller, maybe you create some custom filters too. At that point you start to think a directive would be better.

Maybe the solution is to make separate modules ?

I'm not good enough in Angular or Javascript to have figured out the best solution yet.

Re: Angular-tips: Consuming services

#14
What happens when you have two controllers sharing the same service in different parts of an app and both of their associated views need to reflect the state of the service?

I don't think a digest loop would be called on the other scope if they are siblings rather than descending from each other.

Maybe this isn't a real problem, but it's something I've wondered about when it comes to this sort of thing.

Post reply on HN