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 :
- $(function() {
- // do some stuff
-
- $('body').trigger('jobDone');
- });
I have others scripts which bind handler on the "jobDone" custom event :
- $(function() {
- $('body').bind('jobDone', null, function() {
- // do something
- });
- });
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 :
- $(function() {
- // do some stuff
-
- $('body').trigger('jobDone');
- $('body').data('jobDoneFired', true);
- });
other scripts :
- $(function() {
- var h = function() {
- // do something
- }
- if ($('body').data('jobDoneFired') == true) {
- h();
- } else {
- $('body').bind('jobDone', null, h);
- }
- });
Is there a better solution ?
Thanks for help.