Eventing
Our previous approach – that is, dealing with component communication – was direct and really static. We need to store the component reference we want to communicate with and write very component-specific code when we want to send a message to it. In JavaScript, there is a new way of communicating, and it's called eventing.
Let's consider this example; the light that's passed to you by your friend is a way for you to receive events from your friend. In JavaScript, we can have objects that have the ability to emit events. By emitting events, we can create a new way of communicating between our objects. This is also called the observer pattern. The following diagram depicts the observer pattern:
Figure 9.2: The observer pattern
In this pattern, instead of calling specific methods in our component, the component that wants to initiate communication will simply emit an event. We can have multiple observers that observe events...