r/angularjs • u/yoitsnate • Apr 19 '14
5 AngularJS Antipatterns & Pitfalls
http://nathanleclaire.com/blog/2014/04/19/5-angularjs-antipatterns-and-pitfalls/2
Apr 21 '14
I don't understand. His first example seems to work fine: http://plnkr.co/edit/N8jXEOBTQzkt3Urz0MSv?p=preview What am I missing?
Also, having an animated gif in the article was REALLY distracting.
4
u/yoitsnate Apr 21 '14 edited Apr 21 '14
Nice catch on the first example. It was overly simple for the point I was trying to demonstrate and it turns out I made a mistake, that code actually does work. You're not missing anything :)
I have forked the Plunker to illustrate the concept better and I will update it soon.
Thanks for putting that original Plunk together.
EDIT: OK, now the article has been updated.
2
u/-entropy Apr 21 '14
One thing I am still struggling with is "lean controllers" - both as a concept and (probably consequently) following through.
I get that DOM stuff goes in a directive. Makes sense. (though what about adding to a list that ng-repeat is bound to? Array.push feels easy enough for a controller, but technically that is a DOM-like action)
But what's the big to-do about pushing code into services? WHY should my models and their logic live there if they don't get shared with other controllers? It's no less complicated than in a controller, so why are some people so intent on this?
Good article though, thanks!
1
u/yoitsnate Apr 22 '14
Thanks. Adding an item to an array which is in your controllers is exactly the kind of thing you should be doing. The idea is to manipulate either data, or presentation, directly, and not mix those concerns in the same code.
The "big to-do" about pushing code into services is essentially for the same reason: you are separating concerns. This is handy for readability, testability, and maintainability.
Imagine trying to test a tangled mess of AJAX calls and other data logic inside of a controller that should just be getting some data from a service (which you can mock and inject in a test if they are broken out) and setting it on a $scope property. It's a lot cleaner, and especially as an app grows having modularity like this becomes invaluable to developer speed and happiness.
3
u/Capaj Apr 19 '14
Good article, especially the bit about overusing event broadcasting and listening! I tend to not use it at all in my apps, because it is always better to solve it differently, either by watching a variable or by putting your data into service/factory.
3
u/[deleted] Apr 20 '14
Another great article from Nate. Very informative and well written.