joshburgess/elm-route-craft/1.0.0

App

Top-level application wiring.

Takes routes, pages, shared state configuration, and produces a Browser.application Program.

Usage

main : Program Flags Model Msg
main =
    App.create
        { init = init
        , router = router
        , toRoute = toRoute
        , toPage = toPage
        , notFound = notFoundView
        , shared =
            { init = sharedInit
            , update = sharedUpdate
            , subscriptions = sharedSubscriptions
            }
        , layout = \_ _ -> Layout.none
        }

Types

type alias Config flags shared sharedMsg route pageModel pageMsg =
{ router : Router route
, toRoute : Url -> route
, toPage : route -> shared -> PageView shared pageModel pageMsg
, shared :
{ init : flags -> ( shared, Cmd sharedMsg )
, update : sharedMsg -> shared -> ( shared, Cmd sharedMsg )
, subscriptions : shared -> Sub sharedMsg
}
}

Configuration for creating an App.

  • router: the Route.Router for URL parsing
  • toRoute: function that determines the current route from a URL, with a fallback
  • toPage: given a route and shared state, produces the page view
  • notFound: view shown for unmatched URLs
  • shared: shared state init/update/subscriptions
type Model shared pageModel

The application model.

type Msg sharedMsg pageMsg
= UrlRequested UrlRequest
| UrlChanged Url
| SharedMsg sharedMsg
| PageMsg pageMsg

The application message type.

type PageView shared pageModel pageMsg

A page view bundles the page's init/update/view/subscriptions in a uniform type. Created via App.page.

Creating an App

Config flags shared sharedMsg route pageModel pageMsg
-> Program flags (Model shared pageModel) (Msg sharedMsg pageMsg)

Create a Browser.application from the config.

This is the main entry point. It wires together routing, page lifecycle, shared state, and the Elm runtime.

main =
    App.create { ... }
{ init : shared -> ( pageModel, Cmd pageMsg )
, update : pageMsg -> pageModel -> ( pageModel, Cmd pageMsg )
, view : shared -> pageModel -> Html pageMsg
, subscriptions : shared -> pageModel -> Sub pageMsg
}
-> PageView shared pageModel pageMsg

Create a PageView from a page's functions and model/msg wrappers.

toPage : Route -> Shared -> PageView Shared PageModel PageMsg
toPage route shared =
    case route of
        Home ->
            App.page
                { init = \_ -> ( (), Cmd.none )
                , update = \_ m -> ( m, Cmd.none )
                , view = \_ _ -> Html.text "Home"
                , subscriptions = \_ _ -> Sub.none
                }