@@ -60,8 +60,8 @@ initialCommand =
6060 Cmd . none
6161
6262
63- init : ( Model , Cmd Msg )
64- init =
63+ init : () -> ( Model , Cmd Msg )
64+ init _ =
6565 ( initialModel, initialCommand )
6666```
6767
@@ -150,9 +150,18 @@ and giving it an alias of `Decode` to make things a little easier to type.
150150Now that we have our libraries imported, the first step we need to take is to
151151make an HTTP GET request to our endpoint. If we take a look at the
152152documentation for the
153- [ ` Http.get ` ] ( http://package.elm-lang.org/packages/elm-lang/http/latest/Http#get )
154- function, we see that this function takes a string value for the URL as the first
155- argument and a JSON decoder as the second argument.
153+ [ ` Http.get ` ] ( https://package.elm-lang.org/packages/elm/http/latest/Http#get )
154+ function, we see that we can use it with a ` url ` value and an ` expect ` value.
155+
156+ The ` url ` will be a string value like ` http://localhost:4000/api/games ` to hit
157+ our Phoenix JSON endpoint, or you could fetch data from a public API like the
158+ Star Wars API at ` https://swapi.co/api/films/1 ` . The ` expect ` value allows us
159+ to use things like ` expectString ` if we're fetching a text file or we can use
160+ ` expectJson ` since we're fetching JSON data.
161+
162+ In other words, we're going to use ` Http.get ` to hit our JSON endpoint, we'll
163+ handle the response using ` FetchGamesList ` in our ` update ` function, and we'll
164+ create a new JSON decoder to decode the game data.
156165
157166Keep in mind that the order of your function declarations doesn't matter in
158167Elm, but I like to add a new section for API functions below the model section.
@@ -162,24 +171,18 @@ to our `"/api/games"` route (and we'll get to the decoding soon).
162171``` elm
163172-- API
164173
165- fetchGamesList =
166- Http . get " /api/games" decodeGamesList
167- ```
168-
169- We also want to trigger this request using the
170- [ ` Http.send ` ] ( http://package.elm-lang.org/packages/elm-lang/http/latest/Http#send )
171- function, so we'll pipe the results of our ` Http.get ` request to ` Http.send `
172- and use ` FetchGamesList ` in our ` update ` function. We'll also add a type
173- annotation to indicate that we're creating a command, which we'll later use
174- in our ` initialCommand ` function when our application starts.
175-
176- ``` elm
177174fetchGamesList : Cmd Msg
178175fetchGamesList =
179- Http . get " /api/games" decodeGamesList
180- |> Http . send FetchGamesList
176+ Http . get
177+ { url = " /api/games"
178+ , expect = Http . expectJson FetchGamesList decodeGamesList
179+ }
181180```
182181
182+ Note the return value for this function is a ` Cmd Msg ` , which we'll later use
183+ in our ` initialCommand ` function so that our application fetches the data as it
184+ initializes.
185+
183186There's a lot going on here, so don't worry if it's getting a little bit
184187confusing. It will all make more sense as we finish up with this initial
185188feature and then we'll do something similar for fetching our player data, which
@@ -248,8 +251,10 @@ request to our JSON endpoint and decode the response:
248251``` elm
249252fetchGamesList : Cmd Msg
250253fetchGamesList =
251- Http . get " /api/games" decodeGamesList
252- |> Http . send FetchGamesList
254+ Http . get
255+ { url = " /api/games"
256+ , expect = Http . expectJson FetchGamesList decodeGamesList
257+ }
253258
254259
255260decodeGamesList : Decode .Decoder (List Game )
@@ -370,15 +375,19 @@ experience as we do the same for our list of players.
370375
371376![ JSON API Player Data] ( images/phoenix_api/player_data_api_scope.png )
372377
373- We can start by adding to our ` Model ` type alias and ` initialModel ` :
378+ We can start by adding to our ` Model ` type alias:
374379
375380``` elm
376381type alias Model =
377382 { gamesList : List Game
378383 , playersList : List Player
379384 }
385+ ```
380386
387+ And we'll also update our ` initialModel ` to use an empty list for the initial
388+ ` playersList ` value:
381389
390+ ``` elm
382391initialModel : Model
383392initialModel =
384393 { gamesList = []
@@ -431,8 +440,10 @@ you can check out!
431440``` elm
432441fetchPlayersList : Cmd Msg
433442fetchPlayersList =
434- Http . get " /api/players" decodePlayersList
435- |> Http . send FetchPlayersList
443+ Http . get
444+ { url = " /api/players"
445+ , expect = Http . expectJson FetchPlayersList decodePlayersList
446+ }
436447
437448
438449decodePlayersList : Decode .Decoder (List Player )
0 commit comments