angular-styleguide icon indicating copy to clipboard operation
angular-styleguide copied to clipboard

Add Angular1 Event Patterns/Best Practices

Open jongunter opened this issue 7 years ago • 7 comments

It would useful if the Angular1 style guide could provide some opinionated patterns on $broadcast/$emit events. Looking at code samples all over the web, I see people doing things very differently. Questions that the guide might want to address:

  • Naming: What's a good strategy for naming/namespacing events?
  • When to use:: When is using these types of events a code smell? When is it advantageous?
  • There's probably more, but I can't think of any right now...

I know a lot of these older event patterns are fading into the sunset in light of RxJs, Flux design patterns, and the like, but some of us still have to maintain "legacy" applications that use $broadcast and $emit.

Thanks. I don't know what I'd do without this style guide in my day-to-day Angular development! It's amazing. Just wanted to address a gap that I noticed.

jongunter avatar Jan 19 '17 23:01 jongunter

When to use

TLDR : Never.

It is a highway for spaghetti source code.

Never ever use them. This is the best practice regardings events.

There is nothing you couldn't do with a service or a directive contract regarding events.

And more, using events means injecting $rootScope and $scope. It is by itself a bad practice.

MarcLoupias avatar Apr 07 '17 13:04 MarcLoupias

We've (teaching classes) have been describing scope events as a pseudo-deprecated feature for a while now. It would be great for the Papa style guide to say something like that.

kylecordes avatar Apr 07 '17 14:04 kylecordes

Good to know. Thank you @MarcLoupias and @kylecordes .

Are there any articles/resources you'd recommend showing best practices for implementing common pub/sub patterns with services?

jongunter avatar Apr 07 '17 22:04 jongunter

@jongunter I don't use AngularJS 1.x much anymore, but I do encounter large customer projects written in it. My advice for those projects as they try to forward to the most modern 1.x practices in preparation for a hopeful eventual move to Angular (2... 4... 5... 6...) is to pick up tools from their likely future and use those.

Your example of wanting to do pub/sub in the service fits that very nicely. Simply adopt RxJS, and use a Subject exposed one of your services. You can think of a Subject as (among many other things) a pub-sub channel ready to go in one line of code.

kylecordes avatar Apr 08 '17 13:04 kylecordes

@jongunter Slightly relevant, I put this together for other purposes.

https://github.com/OasisDigital/angularjs-rxjs-example

(There is a link to a video in the README, that explains the code in depth.)

kylecordes avatar Apr 10 '17 04:04 kylecordes

Can I ask a question of people advising against events? I have two components that need to talk to each other (eg, when you click on something in ComponentA, it needs to filter stuff in ComponentB). Currently, ComponentA fires an event that ComponentB listens to, but I agree, it's resulted in some hard-to-decipher code. How do you recommend handling situations like that?

mattgrande avatar Apr 10 '17 13:04 mattgrande

Worthy of being considered a standard recommended style?

If the two components are closely adjacent (one is a parent of the other, or maybe they are siblings whose parent manages a feature that both of the components participate in), use ordinary data binding and event bindings to communicate among them.

If the two components are farther apart in the application, they can communicate most effectively by both injecting a service of some kind. That way the relationship between them is explicit and visible via the injection mechanism, rather than implicit based on commonly named events. Thi also avoids the "bucket brigade" that occurs if you use data binding an event binding to wire up far-flung components.

Beyond style guide scope

What kind of service should it be? I think that's far outside the scope of the style guide. I would typically say: it should be a reactive service, or in an app of some complexity, probably Redux or similar.

kylecordes avatar Apr 10 '17 13:04 kylecordes