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

Moving $emit, $broadcast, or $on to a factory? #398

Closed
kdekooter opened this issue May 12, 2015 · 8 comments
Closed

Moving $emit, $broadcast, or $on to a factory? #398

kdekooter opened this issue May 12, 2015 · 8 comments
Labels

Comments

@kdekooter
Copy link

John, in Y031 you mention the following:

For example when publishing and subscribing events using $emit, $broadcast, or $on consider moving these uses to a factory and invoke from the controller.

Could you expand a bit on how to implement this? A code sample would be nice!

@meldaravaniel
Copy link

I wondered this too and found the following stackOverflow question/answer: http://stackoverflow.com/questions/14056874/how-to-emit-events-from-a-factory

I'm fairly beginner as far as angular goes, so if this is bad practice, someone please tell me. :)

@johnpapa
Copy link
Owner

great insight here http://stackoverflow.com/questions/11252780/whats-the-correct-way-to-communicate-between-controllers-in-angularjs/19498009#19498009

@zdjohn
Copy link

zdjohn commented Aug 26, 2015

I think it is also depending on whether you are in a pure angular project or a legacy/hybrid project.
i.e. you want to share same event inside and out side Angular framework
making the pub/sub into a factory make alternative implementation easier to replace $emit, $broadcast.
e.g. http://jonathancreamer.com/an-angular-event-bus-with-postal-js/

@sebastianhaas
Copy link

I'm too having some trouble understanding the following sentence from Y031:

Consider using $scope in a controller only when needed. For example when publishing and subscribing events using $emit, $broadcast, or $on consider moving these uses to a factory and invoke from the controller.

So the conclusion of the above sentence is to avoid broadcasts at all with the use of factories? If so, I'd suggest a different formulation.

I'm not thinking about inter-controller communication but rather of major state changes like login/logout. Since I possibly have to inform pretty much all components about that if feels like a proper task for a broadcast?

@zachlysobey
Copy link
Contributor

If I understand correctly, I think you are misunderstanding this @sebastianhaas. This passage in the style-guide is suggesting moving the usage of $emit, $broadcast, or $on to a factory, not replacing the usage with a factory.

@sebastianhaas
Copy link

Thanks for your answer @zachlysobey! Yes, but how would you achieve that? Lets assume I want to update values in a controller on certain broadcasts. If I move those $on's to a factory, I'm wondering how to inform the controller again.

@johnpapa
Copy link
Owner

johnpapa commented Oct 7, 2015

Let me clarify ... I had a confusing sentence in there. The point was to only use $scope when needed. Which is when you have to have a scope to handle the job. This includes broadcast or emit. Those are OK in the controller if, and only if, a message is what you need to send. But you may also want to consider other options too.

I was trying to be careful here because I want to discourage scope.apply in controllers too.

I removed the sentence.

@johnpapa johnpapa closed this as completed Oct 7, 2015
@sebastianhaas
Copy link

Thank you very much!

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

No branches or pull requests

6 participants