javascriptangularjsangularjs-directivedom-eventsangularjs-service

Receive events with Angular in order


I have some trouble managing events in AngularJs. In a service I dispatch events with the following line of code :

 $rootScope.$emit("FNH."+this.status, flowNodeHelper);

In a service "S1" I receive the event in the following way :

    $rootScope.$on('FNH.READY', function(event, flowNodeHelper) {
        flowNodeHelperService.setActive(flowNodeHelper);
    });
    $rootScope.$on('FNH.ACTIVE', function(event, flowNodeHelper) {
        flowNodeHelperService.setCompleting(flowNodeHelper);
    });
    $rootScope.$on('FNH.COMPLETING', function(event, flowNodeHelper) {
        flowNodeHelperService.setCompleted(flowNodeHelper);
    });
    $rootScope.$on('FNH.COMPLETED', function(event, flowNodeHelper) {
        flowNodeHelperService.setClose(flowNodeHelper);
    });

In a directive I receive event in the same way :

     $rootScope.$on('FNH.READY', function(event, flowNodeHelper) {
           console.log(flowNodeHelper + " status is ready");
      });
      $rootScope.$on('FNH.ACTIVE', function(event, flowNodeHelper) {
         console.log(flowNodeHelper + " status is  active");
      });
      $rootScope.$on('FNH.COMPLETING', function(event, flowNodeHelper) {
          console.log(flowNodeHelper + " status is completing");
      });
      ...

In my service "S1" when I receive event "FNH.READY", I call the flowNodeHelper.setCompleting() function which dispatch an event of type FNH.ACTIVE with $rootScope.$emit. Same thing to go from ACTIVE status to COMPLETING status, etc. workflow is going on.

In "S1" I receive events in the good order READY, then ACTIVE, then COMPLETING, ... But in my directive I receive them in the other order : COMPLETED then COMPLETING, then ACTIVE, then READY !!

How can I do to receive events in the same time in my "S1" service and in my directive please ?


Solution

  • Strange, it works if I do the following for each call to flowNodeHelperService in S1 :

    $timeout(function() {
         flowNodeHelperService.setActive(flowNodeHelper);
    }, 1);