Dispatching events to a .subscribe JavaScript method -
i've been stuck on , i'm coming guys last resort. have javascript file (parent.js) loading .js file (child.js). parent.js file has event subscription:
class.prototype.subscribe('someevent', someeventhandler)
very similar facebook does:
fb.event.subscribe('auth.authresponsechange', auth_response_change_callback); fb.event.subscribe('auth.statuschange', auth_status_change_callback);
i'm trying understand how child.js can dispatch (or broadcast) someevent someeventhandler gets fired.
it seems see tells how add listener, nothing how dispatch event handle method attached listener.
references: api-docs https://developers.facebook.com/docs/reference/javascript/fb.event.subscribe/v2.3
someone similar resolved issue, i'm not using fb sdk, using example make objective clearer:
fb.event.subscribe not firing events
event dispatching in general: https://developer.mozilla.org/en-us/docs/web/guide/events/creating_and_triggering_events
thanks ton in advance.
it appears though might using sort of library wasn't mentioned.
with said, here example of using custom events html , pure javascript.
html...
<button onclick="triggerthething()">trigger thing</button> <br /> <span id="listeningelement1">listening element</span>
and javascript...
myelement = document.getelementbyid("listeningelement1"); myelement.addeventlistener("triggeredevent", function (e) { this.innerhtml = e.detail.message; }); function triggerthething() { var newevent = new customevent("triggeredevent", { detail: { message: "it triggered!" } }); myelement.dispatchevent(newevent); }
one thing tripped me while learning need dispatch event on element listening event.
here's jsfiddle of in action: http://jsfiddle.net/4z891lwl/
props guy: http://davidwalsh.name/customevent