I’m stuck a point where I want to display a list from a submodule that produces an output of type “Element Msg” along with data from the main module that produces also “Element Msg”. The issue is that the Msg in both cases is of different types. How do I mix both types?
Thanks for hinting me in the right direction and providing an example. I was able to use Element.map resolve the issue.
Based on this question I would like to know how to best change the model (state) within a submodule. For this I would have to call the update of the submodule from the main. Or should basically only one model (state) be used in the Main?
I don’t know if it’s the best way, but one way to do it is the following pattern where you have a State, init, Msg and update in your module and using them in main like this:
import Module
type alias Model =
{ moduleState : Module.State }
init : Model
init =
{ moduleState = Module.init }
type Msg
= FromModule Module.Msg
update : Msg -> Model -> Model
update msg model =
case msg of
FromModule moduleMsg ->
{ model
| moduleState =
Module.update
moduleMsg
model.moduleState
}
import ModuleA
import ModuleB
type Msg = FromA ModuleA.Msg | FromB ModuleB.Msg
subscriptions : Model -> Sub Msg
subscriptions model =
Platform.Sub.batch
[ Platform.Sub.map FromA ModuleA.subscription model.statusA
, Platform.Sub.map FromB ModuleB.subscription model.statusB
]
…but I doubt it’s advisable. The note on Platform.Sub.map seems to think it is seldom useful in “well-structured Elm code”. But I’m not sure what a better alternative looks like.
I see your point and decided to only export (Model, Msg(..), init, update, view) from my submodules. This means that I had to move subscriptions from submodules to Main.