How to call a parent msg from component in Elm? -


i have modal window can display different components inside it. each component has it's own updater , messages, want share close button between them.

thus can't call "closemodal" directly children — elm doesn't allow me call else messages. options?


i thought call "modal.update.update modal.messages.closemodal", inside components have chunks of state. it's not option.

then found way pass messages parent child, doesn't me pass messages other way around. or siblings.

in short, can not pass messages directly child parent or sibling.

elm architecture implements uni-directional message passing, in other words, parent component aware of messages child components before child component receive message.

i have made simple example of parent-child communication, way big embed answer note key points here.

child

child component defines set of messages:

type msg     = update model     | focus     | blur 

in it's update function ignore messages, intended parent component.

update : msg -> model -> ( model, cmd msg ) update msg model =     case msg of         update value ->             ( value, cmd.none )          -- ignore rest of messages.         _ ->             ( model, cmd.none ) 

parent

in parent's update function can pattern match required messages , react them.

the rest of messages go through default branch.

update : msg -> model -> ( model, cmd msg ) update msg model =     case msg of         namemsg childmsg ->             case childmsg of                 {- have intercepted message child component.                    part of update function might moved                    separate function better readability.                 -}                 input.focus ->                     update (helpermsg helper.show) model                  input.blur ->                     update (helpermsg helper.hide) model                  -- default message passing routine.                 _ ->                     let                         ( namemodel, namecmd ) =                             input.update childmsg model.name                     in                         ( { model | name = namemodel }                         , cmd.map namemsg namecmd                         ) 

the example above concludes child-parent , sibling communication. can run update function recursively as want messages components.

sending messages child's update function

cmd.extra exposes function sending messages.

update : msg -> model -> ( model, cmd msg ) update msg model ->     (model, message somemessage) 

ps: translator pattern example on to-do, leave comment if want me update answer it.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -