'Index-file to module-instance interaction
I am making a computer system which I have broken down into several smaller parts.
From a given experience, it is crucial in software development to keep systems small.
In order to do that I am implementing a module which serves a specific purpose, all and by itself without having any idea what the rest of the system is doing. (ideal)
So this module goes there and does its thing, and when an event occurs in an instance of this module, I want the index file to become aware of that happening. I do not want this module to communicate directly to the other modules, as they should not communicate with each other, thereby decreasing dependencies within the system.
Now, I have made the instance and the instance does everything right, but when an event occurs in this instance, how do I get that information over to the index file?
An event of this instance is implemented in the following manner:
the_div.addEventListener('mousedown',this.react_to_mouse_down_function);
And here its a question how its best to get this happening to the index file, I can do in the index file:
the_div.addEventListener('click', the_function);
but that forces the index file to be aware of things going on in the instance because the instance has children which each has event detector and their numbers can grow and shrink and god knows what. It would be best if the index file wouldn't need to understand how the instance of the module works, and thereby not needing to be generating new event listeners on the fly as the instance is living.
How is it best to setup the interaction between the index file and this instance?
Solution 1:[1]
This sounds like a pub/sub problem. This would let the module publish a specific custom event when state changes and the index (or any other module) can subscribe to that event if needed. The module does not communicate with anything besides the pub/sub controller.
Here is a good link: http://addyosmani.com/resources/essentialjsdesignpatterns/book/#observerpatternjavascript
I find myself referring to various sections of this page often.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Malk |
