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

Why not just?

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

clouddueling avatar Oct 15 '13 15:10 clouddueling

Because of DRY :)

cburgdorf avatar Oct 15 '13 17:10 cburgdorf

DRY of course :) and there may be more than two events. The one-shot deregistration function would be fine in that case

lcanet avatar Oct 15 '13 19:10 lcanet

I fell into that :P I meant if there was something else I wasn't seeing. I have ran into a need for this "multiple event listener" many times.

clouddueling avatar Oct 15 '13 20:10 clouddueling

I could submit a patch for this, but how would you propose returning the 'unregister' function? would it unregister each of the registered events? or return an array of unregister functions? I'm not sure!

caitp avatar Oct 17 '13 16:10 caitp

@caitp return a function that calls an array of unregister functions inside. Just don't use promises.

matsko avatar Oct 17 '13 16:10 matsko

Patch submitted, I don't know if this sort of thing would be merged, or if it meets the use case requirements that you want, so by all means please review when/if you have time

caitp avatar Oct 17 '13 16:10 caitp

perfect ! hope it will be merged

lcanet avatar Oct 17 '13 19:10 lcanet

I think you might be able do this already. http://plnkr.co/edit/Rmap3aIgAek2JWWAMeF9?p=preview

By a fluke I found this guy's example and extended it. http://blog.tomaka17.com/2012/12/random-tricks-when-using-angularjs/

clouddueling avatar Oct 18 '13 22:10 clouddueling

@clouddueling this doesn't demonstrate registering a listener for multiple events in a single call, this demonstrates watching the concatenated value of 3 variables in scope.

caitp avatar Oct 18 '13 23:10 caitp

Hope this will be added to the next version

julien-rodrigues avatar Oct 24 '13 08:10 julien-rodrigues

I like space delimited string syntax better. Keeps it inline with that other popular JS project: $scope.$on('authfailed $routeChangeStart', function(event, data) { ... }

eddiemonge avatar Dec 19 '13 20:12 eddiemonge

+1

eddiemonge avatar Dec 19 '13 20:12 eddiemonge

what is the current state of this pr ? is it going to be merged anytime soon ?

lucianenache avatar Feb 24 '14 13:02 lucianenache

@lucianenache the main problem with it is the return value of $on(), still need to figure out if it's the right thing to do to deregister all of the events in one call

caitp avatar Feb 24 '14 19:02 caitp

Small comment (I know I'm late to the party on this one) - would it make more sense to do this space-separated, like how jQuery and other popular libraries handle multiple events?

E.g. $scope.$on('sizeChanged visibilityChanged', myListener);

It's a little cleaner, IMO.

quantizor avatar Feb 25 '14 04:02 quantizor

that's what we're doing...

caitp avatar Feb 25 '14 04:02 caitp

Derp. That's what I get for only reading the first few replies. Sorry :)

quantizor avatar Feb 25 '14 04:02 quantizor

I didn't mean to sound angry or anything :( but yeah, that's basically what it comes down to right now... The place where the bitrotten patch is iffy is the deregistration functions

caitp avatar Feb 25 '14 04:02 caitp

Not at all! Caffeine has totally left my system at this point of the evening, heh.

quantizor avatar Feb 25 '14 04:02 quantizor