How is child/submodel to parent/main model messaging done in Elmish.WPF?

223 views Asked by At

(Newbie question).

In Elmish.WPF, I have a parent module, App, holding a child submodule, FinderLastName:

module App =

    type Model =
        { 
            FinderLastName:  FinderLastName.Model      
        }

    type Msg =
        | FinderLastNameMsg  of FinderLastName.Msg
       

    let update msg m =
        match msg with
        | FinderLastNameMsg msg ->
            { m with FinderLastName  = FinderLastName.update msg m.FinderLastName}
       
    let bindings () : Binding<Model, Msg> list = [
        "FinderLastName" |> Binding.subModel(
          (fun m -> m.FinderLastName),
          snd,
          FinderLastNameMsg,
          FinderLastName.bindings)
     ]

The subModel, FinderLastName , receives a text input from the UI which I need to transmit up to the main App module.

Assuming the subModel, FinderLastName, has the typical Elmish.WPF structure of Model, Msg, and update, how do I transmit the text input from child/subModel to parent/main Model?

(I found a good discussion of this for Elm-spa, but how would this be used in Elmish.WPF? https://discourse.elm-lang.org/t/modifying-parent-state-from-child-page-in-an-elm-spa-example-like-architecture/2437)

Any ideas would be most appreciated.

TIA

Found it! Please see the really good blog at: https://medium.com/@MangelMaxime/my-tips-for-working-with-elmish-ab8d193d52fd

About 3/4 of the way down, Make the child communicate with the parent

Addendum: For a good explanation of the event loops used by Elmish, see: https://elmish.github.io/elmish/

2

There are 2 answers

3
TheQuickBrownFox On BEST ANSWER

The Elmish.WPF repo has several good sample projects built in which demonstrate this sort of thing. Look at the projects with "SubModel" in the name.

I suggest you clone the repo and open it in your IDE to get full type inference. You can also run each sample project. I personally think this is a really good form of documentation.

2
Tyson Williams On

OP shared this Elm discussion in this comment. As I said in this comment, I like the translator pattern that was mentioned there. I plan on implementing it in Elmish.WPF soon.