Register multiple events with Scope.$on
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
Why not just?
$scope.$on('sizeChanged', myListener);
$scope.$on('visibilityChanged', myListener);
Because of DRY :)
DRY of course :) and there may be more than two events. The one-shot deregistration function would be fine in that case
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.
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 return a function that calls an array of unregister functions inside. Just don't use promises.
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
perfect ! hope it will be merged
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 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.
Hope this will be added to the next version
I like space delimited string syntax better. Keeps it inline with that other popular JS project: $scope.$on('authfailed $routeChangeStart', function(event, data) { ... }
+1
what is the current state of this pr ? is it going to be merged anytime soon ?
@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
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.
that's what we're doing...
Derp. That's what I get for only reading the first few replies. Sorry :)
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
Not at all! Caffeine has totally left my system at this point of the evening, heh.