+1 about time for a directive. Actually, if is not purely behavioural, if it has visual stuff, then anything would be cleanly pre-refactored if you do it in a directive
-1 about using switch, for me you can take that out of the framework it's only inducing poor design On Sunday, May 25, 2014 10:04:28 PM UTC-3, Eric Marthinsen wrote: > > Hi. I've got an app where I have a list of users. The users come in two > varieties: subscribers and admins (more types are on their way). The > different user types have different behaviors associated with them. I want > to list of all the users and I want to associate them with the correct > behaviors. Here's one implementation that works: > > http://plnkr.co/edit/lHyLxAuF7GYOvN6n4ouc?p=preview > > The core of that example is a UserCtrl and a bunch of conditionals to > determine how to handle each user, based upon its type. > > I'm not crazy about that implementation though. As the number of user > types increases, each conditional needs to be revisited. I feel like it's a > violation of the open-close principle and, overall, it's cumbersome. > > Here's an implementation that I prefer. The only caveat is that it doesn't > work. > > http://plnkr.co/edit/PIrRHfD4tiTHyKpUvdZX?p=preview > > The essence of this solution is that we have a SubscriberCtrl and > AdminCtrl that we dynamically assign. What I really like about this > solution is that it does away with all the conditionals. The behavior for > each user type is encapsulated in a dedicated controller. Accommodating > more user types only involves adding the user type to the switch statement > in MainCtrl. At no point, do existing controllers need to be touched when > new user types are added or when the behavior of a specific user type is > modified. > > I've heard some people say that using one controller per item in ngRepeat > isn't idiomatic, but it feels right. My question is, what is the best way > to accomplish what I'm trying to do? Naturally, it needs to work, but it > should also be idiomatic. > > One finer point about the examples is that the structure of the HTML is > very deliberately chosen. It mirrors the setup in my app and, because of > the necessity of the table, greatly reduces the possibility of introducing > arbitrary elements into the DOM. > > Cheers, > Eric > -- You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
