我已經成功地引發ELM一個簡單的HTTP請求和解碼JSON響應到ELM值 - [https://stackoverflow.com/questions/43139316/decode-nested-variable-length-json-in-elm]鏈HTTP請求和合並JSON響應
的問題我面對NOW-
如何鏈接(併發首選)兩個http請求並將json合併到我的新(更新)模型中。注 - 請更新Commands.elm用於訪問遠程數據
套餐 - krisajenkins /對RemoteData http://package.elm-lang.org/packages/krisajenkins/remotedata/4.3.0/RemoteData
我的代碼GitHub庫 - https://github.com/areai51/my-india-elm
以前工作守則 -
Models.elm
type alias Model =
{ leaders : WebData (List Leader)
}
initialModel : Model
initialModel =
{ leaders = RemoteData.Loading
}
Main.elm
init : (Model, Cmd Msg)
init =
(initialModel, fetchLeaders)
Commands.elm
fetchLeaders : Cmd Msg
fetchLeaders =
Http.get fetchLeadersUrl leadersDecoder
|> RemoteData.sendRequest
|> Cmd.map Msgs.OnFetchLeaders
fetchLeadersUrl : String
fetchLeadersUrl =
"https://data.gov.in/node/85987/datastore/export/json"
Msgs.elm
type Msg
= OnFetchLeaders (WebData (List Leader))
Update.elm
update msg model =
case msg of
Msgs.OnFetchLeaders response ->
({ model | leaders = response }, Cmd.none)
更新的代碼 -(需要Commands.elm幫助)
Models.elm
type alias Model =
{ lsLeaders : WebData (List Leader)
, rsLeaders : WebData (List Leader) <------------- Updated Model
}
initialModel : Model
initialModel =
{ lsLeaders = RemoteData.Loading
, rsLeaders = RemoteData.Loading
}
Main.elm
init : (Model, Cmd Msg)
init =
(initialModel, fetchLeaders)
Commands.elm
fetchLeaders : Cmd Msg
fetchLeaders = <-------- How do I call both requests here ? And fire separate msgs
Http.get fetchLSLeadersUrl lsLeadersDecoder <----- There will be a different decoder named rsLeadersDecoder
|> RemoteData.sendRequest
|> Cmd.map Msgs.OnFetchLSLeaders
fetchLSLeadersUrl : String
fetchLSLeadersUrl =
"https://data.gov.in/node/85987/datastore/export/json"
fetchRSLeadersUrl : String <------------------ New data source
fetchRSLeadersUrl =
"https://data.gov.in/node/982241/datastore/export/json"
Msgs.elm
type Msg
= OnFetchLSLeaders (WebData (List Leader))
| OnFetchRSLeaders (WebData (List Leader)) <-------- New message
更新。榆樹
update msg model =
case msg of
Msgs.OnFetchLSLeaders response ->
({ model | lsLeaders = response }, Cmd.none)
Msgs.OnFetchRSLeaders response -> <--------- New handler
({ model | rsLeaders = response }, Cmd.none)