I have a modal window that can display different components inside it. Each component has it's own updater and messages, but I want to share a close button between them.
Thus I can't call "CloseModal" directly from my children — Elm doesn't allow me to call someone else messages. What are my options?
I thought I could call "Modal.Update.update Modal.Messages.CloseModal", but inside my components I have only chunks of a state. So it's not a option.
Then I found a way to pass messages from parent to child, but it doesn't help me to pass messages other way around. Or to siblings.
In short, you can not pass messages directly from child to parent or a sibling.
Elm Architecture implements uni-directional message passing, in other words, your parent component is always aware of messages for child components before child component will receive a message.
I have made a simple example of parent-child communication, it is way too big to embed it into an answer so that I will note only the key points here.
Child
Child component defines a set of Messages:
In it's
update
function we ignore Messages, intended for the parent component.Parent
In parent's
update
function we can pattern match required messages and react to them.The rest of the messages will go through default branch.
The example above concludes the child-parent and sibling communication. You can run the update function recursively as much as you want with any messages to any components.
Sending Messages from child's
update
functionCmd.Extra exposes a function for sending messages.
PS: Translator pattern example is on my To-do, leave a comment if you want me to update the answer with it.