Plnkr example build: http://plnkr.co/edit/gB7MtVOOHH0FBJYa6P8t?p=preview
Following the answer here, I created some $broadcast
events to allow actions in the main $scope
to close popovers in sub $scopes
. However I want to make sure I clean up all my events and not have anything lingering that should not.
I have a popover directive, once the popover is activated I send out:
vs.$emit('popoverOpen');
Then in the main app module ($rootScope), I listen for it here:
vs.$on('popoverOpen',function(events,data) {
// if 'popoverOpen' is heard, then activate this function
// which on click $broadcasts out even 'bodyClick'
// but also destroy the 'popoverOpen' event
vs.bodyClick = function() {
$rootScope.$broadcast('bodyClick');
$rootScope.$$listenerCount.popoverOpen=[];
};
});
Back in the popover Directive, here is my bodyClick
listener:
vs.$on('bodyClick', function() {
vs.searchPopoverDisplay = false;
$rootScope.$$listenerCount.bodyClick=[];
});
^ I also have code in there attempting to kill the bodyClick
event, however to no avail :(
As you can see below, I've removed bodyClick
and popoverOpen
from the $$listenerCount
(there was never anything in the $$listener
object.
However the user is still able to access the vs.bodyClick()
function in the main app rootScope, even though popoverOpen
should have been removed.
So on first load:
- If the user clicks around without opening any popover,
bodyClick
is never captured / transmitted - After opening a popover,
bodyClick
is active - If the user clicks on the body, that event is sent out and closes the popover
- However, now even with NO popovers open, if the user clicks on the body that
bodyClick
event keeps getting sent out
How would you properly remove the events after the bodyClick
event has been sent out to close the popover?
UPDATE I also tried this (https://github.com/toddmotto/angularjs-styleguide#publish-and-subscribe-events), but still the bodyClick event keeps getting sent out after the popover is closed and supposedly destroyed.
popoverDirective function still gets called:
vs.$on('bodyClick', function() {
console.log('bodyClick received ...');
console.log($rootScope.$$listener);
console.log($rootScope.$$listenerCount);
vs.searchPopoverDisplay = false;
var unbind = $rootScope.$on('popoverOpen', []);
vs.$on('$destroy', unbind);
// $rootScope.$$listenerCount.bodyClick=[];
});