You can use Browser.document
instead as the Elm app takes the whole page
I would put this in the Main.elm
, replacing Msg
. It will avoid the Cmd.map
in the init
for example. In functions in HttpRequests
I would give the message as parameter.
Ex:
listNames : (Result Http.Error (List TopicName) -> msg) -> Cmd msg
listNames toMsg =
Http.get { url = crossOrigin targetHost [ "names" ] [], expect = Http.expectJson toMsg topicNamesDecoder }
With this, your init would look like:
init : () -> ( Model, Cmd Msg )
init _ =
( LoadingNames, listNames GotNames )
It's personal preference but I would prefer result |> Result.map (\count -> ( topicName, count )) |> GotRecordCount
instead of GotRecordCount (Result.map (\count -> ( topicName, count )) result)
Also, I would put everything in the decoder. Having:
loadRecordCount : TopicName -> Cmd Msg
loadRecordCount ((TopicName topicName) as topic) =
Http.get
{ url = crossOrigin targetHost [ "topics", topicName, "records" ] [ string "fields" "count" ]
, expect = Http.expectJson GotRecordCount recordCountDecoder
}
recordCountDecoder topic =
Decode.map (\count -> ( topicName, RecordCount count )) Decode.int
Usually I pattern match only on the msg, and eventually inside again on the model.
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GotNames result -> ...
Maybe, in you case, as you have very different state in your model, I could match first on the model as it's a huge distinction... Anyway, it's just syntax ;)