Note that there are some explanatory texts on larger screens.

plurals
  1. POWaiting for event to be handled to continue executing the function that has triggered the event.
    text
    copied!<p>I'm looking for an elegant, efficient solution to my problem :</p> <p>I have this webapp with many components;</p> <p>One main component include many additions that will grow/evolve with time.</p> <p>This main component has a function in which before actually doing what it is supposed to do, it is triggering an event beforedo so that the addons can listen to.</p> <pre><code>dostg : function () { $doc.trigger('beforedo'); //do stuff but after event is handled by the addons ? } </code></pre> <p>In the addons codes</p> <pre><code>$doc.on('beforedo',function(e) { //do before addon stuff } </code></pre> <p>Now those before doing stuff may involve ajax request or anything that would take some processing time.</p> <p>I could increment a counter on ajax requests and wait it is down to nil, but what I'd like is a solution that would just wait for all handlers to finish their job (and therefore that'd work whatever is being done in those addons handlers).</p> <p>Is there some miracle solution to do this or I have to forget about event in this situation and go another way (array of function to iterate, addons pushing new function into array) ?</p> <p>Thanks for your expertise!</p> <p>-------EDIT FOLLOWING THE BOUNTY</p> <p>Apologize to @xbonez &amp; @Sergiu Paraschiv , I should have edit the question with the solution I'm using now <em>before</em> offering the bounty (solution I'm not totally satisfied with, hence the bounty).</p> <pre><code>//app var $doc = $(document.body); //component $doc.on({ wait: function (e) { ++$doc.component.handleinprogress; }, addonready: function () { --$doc.component.handleinprogress; if ($doc.component.handleinprogress==0) $doc.trigger('readyfordo'); }, readyfordo: function () { //do stuff after event has been handled by the addons } }); $doc.component = { handleinprogress: 0, dostg: function () { $doc.component.handleinprogress = 0; $doc.trigger('beforedo'); } }; //in component addon files $doc.on('beforedo', function (e) { $doc.trigger('wait'); //do addon stuff $doc.trigger("addonready"); } </code></pre> <p>I'm not satisfied with this solution because even if I don't need to do stuff in the addon beforedo I still have to add the handler to trigger addonready (in at least one of the addon -> so either i lose flexibility to add/remove addon from the component without worrying whether readyfordo get triggered, either I have to include the handler in every addon - mostly -75%- for nothing).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload