joshburgess/elm-route-craft/1.0.0

Page

Page lifecycle abstraction.

Pages are values, not modules. You can create them with different complexity levels, pass them around, and compose them.

-- Simplest: view-only, no state
aboutPage =
    Page.static
        { view = \shared -> Html.text "About"
        }


-- With local state:
counterPage =
    Page.sandbox
        { init = \shared -> 0
        , update = \msg model -> model + 1
        , view = \shared model -> Html.text (String.fromInt model)
        }


-- Full page with commands:
blogPage =
    Page.element
        { init = \shared -> ( Loading, fetchPosts shared.apiUrl )
        , update = \msg model -> ( model, Cmd.none )
        , view = \shared model -> Html.text "Blog"
        , subscriptions = \shared model -> Sub.none
        }

Types

type Page shared model msg

An opaque page type. Internally normalizes all page variants to the full init/update/view/subscriptions shape.

Type parameters:

  • shared : the shared state type passed to all page functions
  • model : this page's local model
  • msg : this page's message type
type PageState model
= Loading
| Loaded model
| Failed String

The loading state of a page, used for transition support.

Constructors

static : { view : shared -> Html msg } -> Page shared () msg

Create a static page with only a view function. No model, no messages, no update logic.

aboutPage =
    Page.static
        { view = \shared -> Html.div [] [ Html.text "About us" ]
        }
{ init : shared -> model
, update : msg -> model -> model
, view : shared -> model -> Html msg
}
-> Page shared model msg

Create a sandbox page with local state but no commands.

counterPage =
    Page.sandbox
        { init = \shared -> 0
        , update = \msg model -> model + 1
        , view = \shared model -> Html.text (String.fromInt model)
        }
{ init : shared -> ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg )
, view : shared -> model -> Html msg
, subscriptions : shared -> model -> Sub msg
}
-> Page shared model msg

Create a full page with commands and subscriptions.

blogPage =
    Page.element
        { init = \shared -> ( initialModel, loadPosts )
        , update = updateBlog
        , view = viewBlog
        , subscriptions = \shared model -> Time.every 60000 Tick
        }

Transformations

(msg -> mappedMsg)
-> Page shared model msg
-> Page shared model mappedMsg

Map over a page's message type.

(model -> mappedModel)
-> (mappedModel -> model)
-> Page shared model msg
-> Page shared mappedModel msg

Map over a page's model type.

(model -> mappedModel)
-> (msg -> mappedMsg)
-> (mappedMsg -> Maybe msg)
-> (mappedModel -> Maybe model)
-> Page shared model msg
-> Page shared mappedModel mappedMsg

Map over both a page's model and message types. This is the primary way to wire a page into the application's unified model/msg types.

App.page homeSpec =
    Page.mapBoth HomeModel HomeMsg (\(HomeMsg m) -> m) (\(HomeModel m) -> m) homePage

More commonly used through the App.page helper.

Accessors

init : Page shared model msg -> shared -> ( model, Cmd msg )

Get the init function from a page.

update : Page shared model msg -> msg -> model -> ( model, Cmd msg )

Get the update function from a page.

view : Page shared model msg -> shared -> model -> Html msg

Get the view function from a page.

subscriptions : Page shared model msg -> shared -> model -> Sub msg

Get the subscriptions function from a page.