This is a case I also run into a lot. Haven't yet found a library/ good
examples for how to deal with it.
The pattern I typically use is:
type alias UID = String -- so it is clear when I deal with ID or Reference
type alias Model =
{ artists : List Artist
, albums : List Album
, nextUID : UID -- for creating new items
}
type alias Artist =
{ uid : UID
, name : String
, albums : List UID
}
type alias Album =
{ uid : UID
, name : String
, artists : List UID
}
albumsForArtist : UID -> Model -> Maybe (List Album)
albumsForArtist artistUID model =
getArtist artistUID model -- Maybe Artist
|> Maybe.map (.albums) -- Maybe (List UID)
|> Maybe.map (List.map (\uid -> getAlbum uid model)) -- Maybe (List (Maybe
Album))
|> Maybe.map catMaybes -- Maybe (List Album) (catMaybes is from Exts.Maybe
package)
getArtist : UID -> Model -> Maybe Artist
getArtist artistUID model =
case List.filter (\artist -> artist.uid == artistUID) model.artists of
artist :: _ ->
Just artist
_ ->
Nothing
Whenever I need one or more albums/ artists from the model, (e.g. in a List
view), I use the UID to get a Maybe of some kind.
Getting Nothing triggers a fetch from the server to supply more data.
This way, I try to keep "One source of Truth": any album and any artist is
only stored in 1 place in my model.
And with a flat model, I can easily build navigation/ views where a e.g.
user clicks album, then one of the artists of that album, then one of the
albums of that artist etc etc.
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.