Web 获得;“装载”;来自Elm&x27的状态;远程数据包
下面是我所能想到的最简单的Elm程序,它使用来更好地模拟服务器响应:Web 获得;“装载”;来自Elm&x27的状态;远程数据包,web,elm,Web,Elm,下面是我所能想到的最简单的Elm程序,它使用来更好地模拟服务器响应: 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 : (M
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
}
即使包的状态为
NotAsked
、Loading
、Success
和Failure
,但我从未看到Loading
状态。此程序包何时发送此状态?我将如何使用它?实际上,您的工作是在发送命令时将加载状态放入模型中:
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OnUpdate response -> (log (toString response) response, Cmd.none)
Ask -> (RemoteData.Loading,
Http.getString "https://api.ipify.org"
|> RemoteData.sendRequest
|> Cmd.map OnUpdate)
另请参见此处的示例: