下記のコードの( Loading, getRandomCatGif )は何を意味するのでしょうか??
MorePleaseを実行したときの返り値でしょうか??
また、Modelの状態を監視しているソースなのですが、Cmdの意味やMorePleaseを押した時の挙動がよく分かっておりません。
ご存知の方がいればお聞きしたいです。
elm
1module Main exposing (main) 2 3import Browser 4import Html exposing (..) 5import Html.Attributes exposing (..) 6import Html.Events exposing (..) 7import Http 8import Json.Decode exposing (Decoder, field, string) 9 10 11 12-- MAIN 13 14 15main : Program () Model Msg 16main = 17 Browser.document 18 { init = init 19 , update = update 20 , subscriptions = subscriptions 21 , view = viewDocument 22 } 23 24 25 26-- MODEL 27 28 29type Model 30 = Failure 31 | Loading 32 | Success String 33 34 35init : () -> ( Model, Cmd Msg ) 36init _ = 37 ( Loading, getRandomCatGif ) 38 39 40 41-- UPDATE 42 43 44type Msg 45 = MorePlease 46 | GotGif (Result Http.Error String) 47 48 49update : Msg -> Model -> ( Model, Cmd Msg ) 50update msg model = 51 case msg of 52 MorePlease -> 53 ( Loading, getRandomCatGif ) 54 55 GotGif result -> 56 case result of 57 Ok url -> 58 ( Success url, Cmd.none ) 59 60 Err _ -> 61 ( Failure, Cmd.none ) 62 63 64 65-- SUBSCRIPTIONS 66 67 68subscriptions : Model -> Sub Msg 69subscriptions model = 70 Sub.none 71 72 73 74-- VIEW 75 76 77viewDocument : Model -> Browser.Document Msg 78viewDocument model = 79 { title = "Some cats", body = [ view model ] } 80 81 82view : Model -> Html Msg 83view model = 84 div [] 85 [ h2 [] [ text "Random Cats" ] 86 , viewGif model 87 ] 88 89 90viewGif : Model -> Html Msg 91viewGif model = 92 case model of 93 Failure -> 94 div [] 95 [ text "I could not load a random cat for some reason. " 96 , button [ onClick MorePlease ] [ text "Try Again!" ] 97 ] 98 99 Loading -> 100 text "Loading..." 101 102 Success url -> 103 div [] 104 [ button [ onClick MorePlease, style "display" "block" ] [ text "More Please!" ] 105 , img [ src url ] [] 106 ] 107 108 109 110-- HTTP 111 112 113getRandomCatGif : Cmd Msg 114getRandomCatGif = 115 Http.get 116 { url = "https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=cat" 117 , expect = Http.expectJson GotGif gifDecoder 118 } 119 120 121gifDecoder : Decoder String 122gifDecoder = 123 field "data" (field "image_url" string) 124 125
Haskell ではないので Haskell タグは外してください

回答1件
あなたの回答
tips
プレビュー