Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Angular1 Event Patterns/Best Practices #805

Open
jongunter opened this issue Jan 19, 2017 · 7 comments
Open

Add Angular1 Event Patterns/Best Practices #805

jongunter opened this issue Jan 19, 2017 · 7 comments

Comments

@jongunter
Copy link

jongunter commented Jan 19, 2017

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.

@MarcLoupias
Copy link

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.

@kylecordes
Copy link

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.

@jongunter
Copy link
Author

jongunter commented Apr 7, 2017

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?

@kylecordes
Copy link

@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
Copy link

@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.)

@mattgrande
Copy link
Contributor

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?

@kylecordes
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants