Custom events already sent

Custom events already sent

Hi !

I'm trying to use some custom events but I don't know how to check if a custom event has already been sent.

I have a script "startup.js", which do some stuff then triggers a custom event "jobDone" on the body element (only one time) :

startup.js :
  1. $(function() {
  2.    // do some stuff
  3.    
  4.    $('body').trigger('jobDone');
  5. });

I have others scripts which bind handler on the "jobDone" custom event :

  1. $(function() {
  2.    $('body').bind('jobDone', null, function() {
  3.       // do something
  4.    });
  5. });

My problem is that I don't know if the bind() will be runned before the trigger() or not. If the trigger() happens before the bind(), the handler won't be executed.

I thought about a dirty solution : adding some data on the body element meaning "jobDone has already been fired" and check this data before the bind to know if I have to bind my handler (data is null) or launch the handler (data is not null) :

startup.js :
  1. $(function() {
  2.    // do some stuff
  3.    
  4.    $('body').trigger('jobDone');
  5.    $('body').data('jobDoneFired', true);
  6. });

other scripts :
  1. $(function() {
  2.    var h = function() {
  3.       // do something
  4.    }
  5.    if ($('body').data('jobDoneFired') == true) {
  6.       h();
  7.    } else {
  8.       $('body').bind('jobDone', null, h);
  9.    }
  10. });

Is there a better solution ?

Thanks for help.