Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. module ElmFun where
  2.  
  3. import Http
  4. import Html
  5. import Html (..)
  6. import Html.Attributes (..)
  7. import Html.Events (..)
  8. import Signal
  9. import List
  10. import Result
  11. import String
  12.  
  13. type alias Model =
  14. { num : Float
  15. , str : String
  16. , list : List Float
  17. , serverResponse: String
  18. }
  19.  
  20. type Action
  21. = NoOp
  22. | ChangeNum String
  23. | ChangeStr String
  24.  
  25. view : Model -> Html
  26. view model =
  27. div
  28. []
  29. [ viewForm model
  30. , viewData model
  31. ]
  32.  
  33. viewData : Model -> Html
  34. viewData model =
  35. div
  36. []
  37. ([ div [] [ text ("serverResponse is: "++model.serverResponse) ]
  38. , div [] [ text ("num is "++(toString model.num)) ]
  39. , div [] [ text ("str is "++model.str) ]
  40. , div [] [ text "list is:" ]
  41. ] ++ (List.map (\n -> div [ style [("backgroundColor", "#ddd")]] [ text (toString n) ]) model.list))
  42.  
  43. viewForm : Model -> Html
  44. viewForm model =
  45. Html.form
  46. []
  47. [ label [] [ text "Number: " ]
  48. , input [ id "numInput", on "input" targetValue (Signal.send updates << ChangeNum) ] []
  49. , br [] []
  50. , label [] [ text "String: " ]
  51. , input [ id "strInput", on "input" targetValue (Signal.send updates << ChangeStr) ] []
  52. , br [] []
  53. , button [ type' "button"{--, onClick submitJson--} ] [ text "submit json" ]
  54. ]
  55.  
  56. {--submitJson : Signal.Message
  57. submitJson = -- What goes here ???? -- --}
  58.  
  59. update : Action -> Model -> Model
  60. update action model =
  61. case action of
  62. NoOp -> model
  63. ChangeNum s ->
  64. (case String.toFloat s of
  65. Result.Ok n ->
  66. { model
  67. | num <- n
  68. , list <- [n, n+1.0, n+2.0]
  69. }
  70. Result.Err _ ->
  71. { model
  72. | num <- 0/0 {-- NaN --}
  73. , list <- []
  74. }
  75. )
  76. ChangeStr s ->
  77. { model | str <- s }
  78.  
  79. main : Signal Html
  80. main = Signal.map view model
  81.  
  82. model : Signal Model
  83. model = Signal.foldp update initialModel (Signal.subscribe updates)
  84.  
  85. initialModel : Model
  86. initialModel =
  87. { num = 0
  88. , str = ""
  89. , list = []
  90. , serverResponse = "<should contain the server response, an error, or a waiting message depending on the status of the request>"
  91. }
  92.  
  93. updates : Signal.Channel Action
  94. updates = Signal.channel NoOp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement