Skip to content

Instantly share code, notes, and snippets.

@drathier
Forked from owanturist/Main.elm
Last active June 29, 2019 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drathier/5e200f1534bf2059f4074cf8c633aa16 to your computer and use it in GitHub Desktop.
Save drathier/5e200f1534bf2059f4074cf8c633aa16 to your computer and use it in GitHub Desktop.
type FrontendMsg
= FNoop
| Increment
| Decrement
| OnBackendMsg ToFrontend
update : FrontendMsg -> Model -> ( Model, Cmd FrontendMsg )
update msg model =
case msg of
FNoop ->
( model, Cmd.none )
Increment ->
( { model | counter = model.counter + 1 }
, sendToBackend CounterIncremented
)
Decrement ->
( { model | counter = model.counter - 1 }
, sendToBackend CounterDecremented
)
OnBackendMsg (CounterNewValue newValue) ->
( { model | counter = newValue }
, Cmd.none
)
updateFromBackend : ToFrontend -> Model -> ( Model, Cmd FrontendMsg )
updateFromBackend toFrontend model =
update (OnBackendMsg toFrontend) model
app =
Lamdera.Frontend.application
{ init = \_ _ -> init
, update = update
, onBackendMsg = updateFromBackend
, view =
\model ->
{ title = "Lamdera board app"
, body = [ view model ]
}
, subscriptions = \_ -> Sub.none
, onUrlChange = \_ -> FNoop
, onUrlRequest = \_ -> FNoop
}
@drathier
Copy link
Author

This shows how to reroute msgs from the updateFromBackend function into the normal update function, in case someone wants to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment