Nilesh
Nilesh

Reputation: 6155

Angular: Order of custom event handlers & default handler

In my application, I am defining a custom event for which I wish to have default handler in place. If any controller/service wants to override default handling, they can do so by adding their own handler.

For implementing this scenario, I added default handler on $rootScope & broadcast the event on $rootScope. I am expecting something like following to work.

//defined in .run block
$rootScope.$on('customEvent', defaultHandler);

//defined inside a controller
$scope.$on('customEvent', function customHandler(e){
    // handle event
    e.preventDefault();
});

My problem is since .run block gets executed before any controller is executed, the defaultHandler always gets registered first. As a result, it gets executed first & then the custom handler gets executed. I want the defaultHandler to be executed at the last.

I want to avoid adding my own logic of maintaining event handlers list & ensuring that they execute in desired order. Is there any way of achieving this in angular? Thanks.

Upvotes: 2

Views: 805

Answers (1)

FrailWords
FrailWords

Reputation: 916

The only notion of 'priority' in angular, as far as I understand, comes from 'directives' and in which sequence they are compiled/linked.

A hack way of doing this would be put in the event handlers in a directive in that scope and order them by whatever priority that represents your ordering.

Upvotes: 1

Related Questions