I have quit a lot of javascript widget and I would like to have a good decoupled way to communicate between them. Something like signals and slots in QT probably? Most of my widgets are written in JQuery using widget factory.

What is the correct way to achieve that, without ending with a spaghetti code?


JavaScript has a built in event system. This gives you an easy way of listening to Events and firing.

You then bind EventListeners to the things you want to listen to (click, myCustomEvent, ...)

When your widget is a pure DOM-enhancing/editing-widget. You will often add eventlisteners to the HTML the widget is enhancing.

Non related widgets could subscribe to the document object that is also a EventTarget thus it can also be used to listen to/fire events.

I tend to often use mootools, thus giving me a powerfull Events mixin in my object. No idea how this is done in jQuery :(

| improve this answer | |
  • 1
    thanks for your answer. I have ended up by using a small "pub/sub" library that allows me to listen for hierarchical events (bla.bli for example). – Yann Sagon Jun 27 '14 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.