The model is the representation of the information required. The generator provides you with an empty one, along with an object literal which you will use for the first render. See types for more information.
type alias Model= {}initialModel: ModelinitialModel= { }
Update
Here we define two things: firstly, the union type that represents all possible actions, which in this case we called Msg. Secondly, the update function which takes in an action message, the current model, and a callback for sending messages from async operations. It returns a new version of the model to be used.
type Msg= Noopupdate: Msg -> Model -> (Msg -> void) -> Modelupdate msg model send =case msg of Noop -> model
View
We also define a view function, which takes a model and returns html created by the html package functions.
view: Model -> HtmlNode Msgview model = div [ ] [ ] [ text "Hello" ]
Root and running
Finally, to hook up the renderer, we grab an element from your index.html file, and use it to contain our Derw program.
root: anyroot= document.getElementById "root"main: RunningProgram Model Msgmain= program { initialModel: initialModel, view: view, update: update, root: root }
Next steps
Imagine you'd like to change the program so that there is a button that when you click on it, it will increment in value and display the value.
Model
First we would want to edit the model so that it contains a value that we can increment.
type alias Model= { currentNumber: number}initialModel: ModelinitialModel= { currentNumber:0 }
Update
Next we'll add a message to our Msg type - to represent when a user has clicked on the button. We'll also add the logic for incrementing the current number to the update function.
type Msg= Noop| Incrementupdate: Msg -> Model -> (Msg -> void) -> Modelupdate msg model send =case msg of Noop -> model Increment -> { ...model, currentNumber: model.currentNumber +1 }
View
We'll also add an onClick listener to the view, and show the current number. Note to do so, we also need to update our import:
import "../derw-packages/derw-lang/html/src/Html" as Html exposing ( HtmlNode, RunningProgram, div, text, program, attribute, class_ )
Now we will be able to refer to the html library via Html
view: Model -> HtmlNode Msgview model =Html.button [ Html.onClick (\_-> Increment) ] [ ] [ text `Current count: ${model.currentNumber}` ]
Finally
That's it! You now have an incrementing button. You code should look something like this:
import "../derw-packages/derw-lang/html/src/Html" as Html exposing ( HtmlNode, RunningProgram, div, text, program, attribute, class_ )
type alias Model= { currentNumber: number}initialModel: ModelinitialModel= { currentNumber:0 }type Msg= Noop| Incrementupdate: Msg -> Model -> (Msg -> void) -> Modelupdate msg model send =case msg of Noop -> model Increment -> { ...model, currentNumber: model.currentNumber +1 }view: Model -> HtmlNode Msgview model =Html.button [ Html.onClick (\_-> Increment) ] [ ] [ text `Current count: ${model.currentNumber}` ]root: anyroot= document.getElementById "root"main: RunningProgram Model Msgmain= program { initialModel: initialModel, view: view, update: update, root: root }