Elm - , - , . , .
HTTP-, Action:
type alias MyThing =
{ id : Int
, name : String
}
type Action
= FetchData
| ErrorOccurred String
| DataFetched MyThing
myDecoder : Json.Decoder MyThing
myDecoder =
Json.object2
MyThing
("id" := Json.int)
("name" := Json.string)
fetchData : Effects Action
fetchData =
Http.get myDecoder url
|> Task.toResult
|> Task.map httpResultToAction
|> Effects.task
httpResultToAction : Result Http.Error MyThing -> Action
httpResultToAction result =
case result of
Ok thing ->
DataFetched thing
Err err ->
ErrorOccurred (toString err)
, , .
JSON myDecoder. JSON Json.Decode.decodeString myDecoder , .
, , , , httpResultToAction. , . , , Result .