I'd like to separate the View and Update parts of a program into separate source files, but then, how do I share the Message and Subscriptions declarations ?
Think twice before splitting your modules, premature code splitting might be harmful to your project.
Here is an example of the project structure I use in my Elm apps.
Html.App.map allows us to tag a message for child component, so we could pass it to Components.Counter.Update.update
function, when keyboard subscription emits a message.
module App.View exposing (..)
import Html.App
import Html exposing (text, div, Html)
import App.Model exposing (Model)
import App.Messages exposing (..)
import Components.Counter.View
view : Model -> Html Msg
view model =
div []
[ Html.App.map Counter (Components.Counter.View.view model.counter) ]
To tag a message from a subscription, we have to use Platform.Sub.map
Please see an example of subscription passing in src/App/Subscriptions.elm
module App.Subscriptions exposing (..)
import App.Model exposing (Model)
import App.Messages exposing (..)
import Components.Counter.Subscriptions
subscriptions : Model -> Sub Msg
subscriptions model =
let
counterSub =
Components.Counter.Subscriptions.subscriptions model.counter
in
Sub.batch [ Sub.map Counter counterSub ]
Main.elm -- Entry point, where App is initialized
Utils.elm -- Utilities
App/
Messages.elm
Model.elm
Subscriptions.elm
Update.elm
View.elm
Components/
StatefulComponent/
Messages.elm
Model.elm
Subscriptions.elm
Update.elm
View.elm
StatefulComponentWithCommands/
Commands.elm -- Functions, which return Cmd msg
Messages.elm
Model.elm
Subscriptions.elm
Update.elm
View.elm
StatelessComponent/
View.elm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With