Skip to content

Instantly share code, notes, and snippets.

@magopian
Created September 5, 2016 15:18
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 magopian/6cd221c7f699100ff9b2cb7fc7394bfd to your computer and use it in GitHub Desktop.
Save magopian/6cd221c7f699100ff9b2cb7fc7394bfd to your computer and use it in GitHub Desktop.
module Main exposing (..)
import Html
import Html.App
import Html.Events
import Http
import Json.Decode as Json exposing ((:=))
import Task
url : String
url =
"http://localhost:8000/"
type alias Model =
{ content : String
}
init : ( Model, Cmd Msg )
init =
( Model "", Cmd.none )
type Msg
= Retrieve
| RetrieveSuccess String
| RetrieveFail Http.Error
update : Msg -> Model -> ( Model, Cmd Msg )
update message model =
case message of
Retrieve ->
( model, getData url )
RetrieveSuccess content ->
( { model | content = content }, Cmd.none )
RetrieveFail error ->
( { model | content = "error" }, Cmd.none )
getData : String -> Cmd Msg
getData url =
Task.perform RetrieveFail RetrieveSuccess (Http.getString url)
view : Model -> Html.Html Msg
view model =
Html.div
[]
[ Html.text ("result: " ++ model.content)
, Html.br [] []
, Html.button [ Html.Events.onClick Retrieve ] [ Html.text "Request data" ]
]
main =
Html.App.program
{ init = init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment