How can I both trigger and bind to an event after the page load?
The problem I am having is that in one place, on page-load I fire an event:
//producer.js
pageLoaded(){
var e = jQuery.Event("eventA");
$("body").trigger(e);
...}
and in another I bind to it:
//consumer.js
pageLoaded(){
$("body").bind("eventA", function(args){console.log("Got it!");} );
... }
The problem is that the first time event is fired (when page is loaded) – the consumer does not see it. The second time event is fired, however, everything is fine!
I am guessing this is because event is actually fired BEFORE consumer starts listening to it.
Is there a good practice to follow when creating “triggers” and “listeners” to events – that have to work both on page load, and later?
Using something like RequireJS will allow me to first load one javascript file, and then another.
So I can make
producer.jsdependent onconsumer.js