Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module ElmFun where
- import Http
- import Html
- import Html (..)
- import Html.Attributes (..)
- import Html.Events (..)
- import Signal
- import List
- import Result
- import String
- type alias Model =
- { num : Float
- , str : String
- , list : List Float
- , serverResponse: String
- }
- type Action
- = NoOp
- | ChangeNum String
- | ChangeStr String
- view : Model -> Html
- view model =
- div
- []
- [ viewForm model
- , viewData model
- ]
- viewData : Model -> Html
- viewData model =
- div
- []
- ([ div [] [ text ("serverResponse is: "++model.serverResponse) ]
- , div [] [ text ("num is "++(toString model.num)) ]
- , div [] [ text ("str is "++model.str) ]
- , div [] [ text "list is:" ]
- ] ++ (List.map (\n -> div [ style [("backgroundColor", "#ddd")]] [ text (toString n) ]) model.list))
- viewForm : Model -> Html
- viewForm model =
- Html.form
- []
- [ label [] [ text "Number: " ]
- , input [ id "numInput", on "input" targetValue (Signal.send updates << ChangeNum) ] []
- , br [] []
- , label [] [ text "String: " ]
- , input [ id "strInput", on "input" targetValue (Signal.send updates << ChangeStr) ] []
- , br [] []
- , button [ type' "button"{--, onClick submitJson--} ] [ text "submit json" ]
- ]
- {--submitJson : Signal.Message
- submitJson = -- What goes here ???? -- --}
- update : Action -> Model -> Model
- update action model =
- case action of
- NoOp -> model
- ChangeNum s ->
- (case String.toFloat s of
- Result.Ok n ->
- { model
- | num <- n
- , list <- [n, n+1.0, n+2.0]
- }
- Result.Err _ ->
- { model
- | num <- 0/0 {-- NaN --}
- , list <- []
- }
- )
- ChangeStr s ->
- { model | str <- s }
- main : Signal Html
- main = Signal.map view model
- model : Signal Model
- model = Signal.foldp update initialModel (Signal.subscribe updates)
- initialModel : Model
- initialModel =
- { num = 0
- , str = ""
- , list = []
- , serverResponse = "<should contain the server response, an error, or a waiting message depending on the status of the request>"
- }
- updates : Signal.Channel Action
- updates = Signal.channel NoOp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement