vendredi 31 mars 2017

Getting the "Loading" status from Elm's RemoteData package

Here is the simplest Elm program I can think of that uses the RemoteData package to better model server responses:

module App exposing (..)

import Http exposing (..)
import Html exposing (..)
import Html.Events exposing (..)
import RemoteData exposing (..)
import Debug exposing (..)

type alias Model = WebData String

init : (Model, Cmd Msg)
init = (RemoteData.NotAsked, Cmd.none)

type Msg = Ask | OnUpdate (WebData String)

view : Model -> Html Msg
view model =
    div []
        [ button [ onClick Ask ] [ text "ask" ]
        , text (toString model)
        ]


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        OnUpdate response -> (log (toString response) response, Cmd.none)
        Ask -> (model,
                Http.getString "https://api.ipify.org"
                    |> RemoteData.sendRequest
                    |> Cmd.map OnUpdate)

subscriptions : Model -> Sub Msg
subscriptions model = Sub.none

main : Program Never Model Msg
main =
    program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }

Even though the package has the states of NotAsked, Loading, Success and Failure, I never see the Loading status. When does this package ever send this status and how would I use it?




Aucun commentaire:

Enregistrer un commentaire