angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Register multiple events with Scope.$on

Open lcanet opened this issue 13 years ago • 52 comments

Be able to bind a listener to multiple events with one call

$scope.$on(['sizeChanged', 'visibilityChanged'], myListener);

The returned deregistration function should remove both of course


PRs: #4492 and #7761

lcanet avatar Oct 15 '13 14:10 lcanet

I like this idea $scope.$on(['sizeChanged', 'visibilityChanged'], myListener); I would like to use it. All in one shot. Hope this will be merged in the next version

henrytao-me avatar Mar 12 '14 10:03 henrytao-me

What about conditionally returning an object containing multiple deregistration functions, keyed by event name?

gabejohnson avatar Mar 13 '14 18:03 gabejohnson

+1 for $scope.$on(['event1', 'event2', 'event3'], myListener) ... Feel like I would use this all the time.

Seems to fit in nicely with the $q.all(['async1', 'async2', 'async3']) syntax.

AlwaysBCoding avatar Mar 21 '14 15:03 AlwaysBCoding

+1 I regularly run into places where this functionality would be so useful and make the code much cleaner :)

aidaeology avatar Apr 10 '14 01:04 aidaeology

+1 This is very in-line with other functionality fx. $q.all. It is very useful in bigger projects.

snorrid avatar Apr 28 '14 15:04 snorrid

+1 Aside of handling many events with one listener it may also have syntax analogous to Object.defineProperties() where one can define event name(s) for a single handler but also register many handlers at a time.

My case is: I'm using events to pass simple information about backend request status in my data access object and I want to log them and to inform user if something went wrong. Now I've written my own function to handle Object.defineProperties()-like syntax but it will be nice to have it just built in https://gist.github.com/jrencz/f1107bb2432672996dbe

jrencz avatar May 16 '14 07:05 jrencz

+1

emmaguo avatar Jul 15 '14 17:07 emmaguo

+1

ananimbus avatar Jul 22 '14 07:07 ananimbus

+1

oliverlukesch avatar Jul 22 '14 10:07 oliverlukesch

+1

liorshk avatar Jul 31 '14 09:07 liorshk

+1

zivc avatar Aug 07 '14 15:08 zivc

+1

AshCoolman avatar Aug 14 '14 11:08 AshCoolman

+1

marcomafessolli avatar Aug 18 '14 08:08 marcomafessolli

+1 :)

amatecha avatar Sep 09 '14 00:09 amatecha

upvote

lekhnath avatar Sep 22 '14 09:09 lekhnath

+1 pls

solidspark avatar Sep 22 '14 17:09 solidspark

+1

Kisama avatar Sep 23 '14 08:09 Kisama

Just a heads up, looks like this feature request won't be making it into core because "this api is used quite rarely"; https://github.com/angular/angular.js/pull/7761

zivc avatar Oct 30 '14 03:10 zivc

I could really use this functionality right now... I think you have a ton of people requesting this which makes this use case not so rare..

niemyjski avatar Nov 04 '14 15:11 niemyjski

This is something that can easily be solved with a short helper method, and not something common enough that it makes sense to include in all apps.

When we add a feature like this, it increases the size of Angular for everyone, regardless of whether they need such a feature. I think it's accurate to say that relative to all apps using Angular, this isn't that common of a need.

You can also add this via a 3rd party module with a decorator as described here if you want such a feature: https://github.com/angular/angular.js/pull/7761#issuecomment-54916632

Because there are so many ways to accomplish this without adding bytes to Angular, I really can't justify merging this into core.

Closing.

btford avatar Nov 04 '14 18:11 btford