Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

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 :(

share|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 at 15:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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