A datatype representing fetched data.
If you find yourself continually using Maybe (Result Error a) to
represent loaded data, or you have a habit of shuffling errors away to
where they can be quietly ignored, consider using this. It makes it
easier to represent the real state of a remote data fetch and handle
it properly.
For more on the motivation, take a look at the blog post [How Elm Slays A UI Antipattern][elm-slays].
To use the datatype, let's look at an example that loads News from a feed.
First you add to your model, wrapping the data you want in WebData:
type alias Model =
{ news : WebData News }Then add in a message that will deliver the response:
type alias Msg
= NewsResponse (WebData News)Now we can create an HTTP get:
getNews : Cmd Msg
getNews =
Http.get decodeNews "/news"
|> RemoteData.asCmd
|> Cmd.map NewsResponseWe trigger it in our init function:
init : ( Model, Cmd Msg)
init =
( { news = Loading }
, getNews
)We handle it in our update function:
update msg model =
case msg of
NewsResponse response ->
( { model | news = response }
, Cmd.none
)And last, we'll render it, correctly handling the various states a web request can be in:
view : Model -> Html msg
view model =
case model.news of
NotAsked -> text "Initialising."
Loading -> text "Loading."
Failure err -> text ("Error: " ++ toString err)
Success news -> viewNews news
viewNews : News -> Html msg
viewNews news =
div []
[h1 [] [text "Here is the news."]
, ...]While RemoteData can model any type of error, the most common
one you'll actually encounter is when you fetch data from a REST
interface, and get back RemoteData Http.Error a. Because that case
is so common, WebData is provided as a useful alias.
Map a function into the Success value.
Map a function into the Failure value.
Map function into both the Success and Failure value.
Chain together RemoteData function calls.
Return the Success value, or the default.
Convert a Result Error, probably produced from elm-http, to a RemoteData value.
Convert a web Task, probably produced from elm-http, to a Cmd (RemoteData e a).
Convert from a Task that may succeed or fail, to one that always
succeeds with the RemoteData that captures any errors.
Append - join two RemoteData values together as though
they were one.
If their value is NotAsked, the result is NotAsked.
If their value is Loading, the result is Loading.
If both values are Failure, the left one wins.
DEPRECATED: Old, incorrect name for append.
State-checking predicate. Returns true if we've successfully loaded some data.
Apply an Elm update function - Model -> (Model, Cmd Msg) - to any Successful-ly loaded data.
It's quite common in Elm to want to run a model-update function, over some remote data, but only once it's actually been loaded.
For example, we might want to handle UI messages changing the users settings, but that only makes sense once those settings have been returned from the server.
This function makes it more convenient to reach inside a
RemoteData.Success value and apply an update. If the data in not
Success a, it is return unchanged with a Cmd.none.
Frequently when you're fetching data from an API, you want to represent four different states: *
NotAsked- We haven't asked for the data yet. *Loading- We've asked, but haven't got an answer yet. *Failure- We asked, but something went wrong. Here's the error. *Success- Everything worked, and here's the data.