Start Elm apps without views
Main.elm
port moduleMainexposing (..)
importWorker{-| We'll receive messages from the world in the form of strings here -}port messagesIn: (String->msg) ->Submsg{-| This port will send our counter back out to the world -}port modelOut:Model->Cmdmsgtype alias Model=Intinit: (Model, CmdMsg)
init =(0,Cmd.none)type Msg=Increment|NoOpupdate:Msg->Model-> (Model, CmdMsg)
update msg model =case msg ofIncrement->(model +1,Cmd.none)NoOp->(model,Cmd.none){-| In this function we define `parse` in order to go fromthe strings that the outside world sends us to the messages ourprogram knows about. We then pass `parse` to `messagesIn` to geta subscription that can update our program from things that happenin JavaScript-land-}subscriptions:Model->SubMsgsubscriptions _ =let
parse value =case value of"Increment"->Increment
_ ->NoOpin
messagesIn parse
{-| The first argument to Worker.worker lets us wrap our updatefunction with additional Cmds to execute on every change. In thiscase we want to send our model out to JS on every update so wepass it our `modelOut` port. We are already receiving messages fromour `messagesIn` port via `subscriptions` so now we're fully connectedto the JavaScript side of the application!-}main:ProgramNevermain =Worker.program modelOut
{ init = init
, update = update
, subscriptions = subscriptions
}app.js
window.addEventListener('loaded',function(){varapp=Elm.Main.worker()app.ports.modelOut.subscribe(function(model){document.getElementById('count').innerHTML=model;})document.getElementById('incrementButton').addEventListener('click',function(){app.ports.messagesIn.send('Increment')})})index.html
<!DOCTYPE html><html><head><metacharset="utf-8" /></head><body><divid="count"></div><div><buttonid="incrementButton">+ 1</button></div><scriptsrc="elm-compiler-output.js"></script><scriptsrc="app.js"></script></body></html>