Radio and PodcastRadio and PodcastLive Radio & Podcasts
Hello Darkness My Old Friend artwork
Technology

Hello Darkness My Old Friend

Javascript to Elm by Jesse Tomchak

Apr 18, 201925:01Technology

Routing in Elm I realize now, just how frustrated people get when learning n framework or language. For me that monster is routing in Elm. But first, you're running out of chances to get to Elm in the Spring!!! Instead o...

About This Episode

Hello Darkness My Old Friend is an episode from Javascript to Elm by Jesse Tomchak. Routing in Elm I realize now, just how frustrated people get when learning n framework or language. For me that monster is routing in Elm. But first, you're...

Podcast

This episode belongs to Javascript to Elm.

Listen Online

Use the player on this page to stream the episode online.

Episode Details

Published Apr 18, 2019, 25:01 long, audio available.

Questions About This Episode

What is Hello Darkness My Old Friend about?

Routing in Elm I realize now, just how frustrated people get when learning n framework or language. For me that monster is routing in Elm. But first, you're running out of chances to get to Elm in the Spring!!! Instead of creating our program with Browser.element or Browser.document , we can create a Browser.application to avoid loading new HTML when the URL changes: ok, cool. Define some routes, in a Routes.elm file preferably in it's own Routes file Define the routes Parser -- creating a parser called 'routes' -- will produce routes of type Route -- doesn't take any arguments? routes : Parser (Route -> a) a routes = Parser.oneOf [ Parser.map Home Parser.top , Parser.map Account (Parser.s "account") ] Define a match function that will convert a Url and return a Maybe Route. Maybe Route because you might not have a matching route, right? I think this is where the default case for pattern matching at one point will show the 404. Be sure we are exposing the Routes(..) and the match function Back on Main.elm create a Browser application import Browser exposing (Document, UrlRequest) import Browser.Navigation as Navigation We need to describe what to display when when we have a specific type Route. This is where Pages come in. Rather than storing a Route Constructor, we store it's commutated value as a 'Page', thus not having to reevaluate the parse route every time? Page and Route have different responsibilities. Page is concerned with the state of the current page, managing just that page's state. Adding Page and navigationKey to model. what's a navigationkey? navigationkey is supplied at runtime. Update Msg with newRoute and Visit New Browser.application onUrlRequest external requests from UrlRequest onUrlChange internal requests to our own domain from UrlRequest >> forward composition operator This is going to compose these 2 functions into 1. The result of Routes.Match will get passed to NewRoute in the update for us to handle. setNewPage helper method Init needs to be a function now, bc it now supplies the inital Url when the application starts up. [==================================================] - 1 / 1-- TYPE MISMATCH ------------ /c/Users/jtomc/Documents/Elm/picshare/src/Main.elm The 1st argument to `application` is not what I expect: 96| Browser.application 97|> { init = init 98|> , view = view 99|> , update = update 100|> , subscriptions = subscriptions 101|> , onUrlRequest = Visit 102|> , onUrlChange = Routes.match >> NewRoute 103|> } This argument is a record of type: { init : () -> Url -> Navigation.Key -> ( Model, Cmd Msg ) , onUrlChange : Url -> Msg , onUrlRequest : UrlRequest -> Msg , subscriptions : Model -> Sub Msg , update : Msg -> Model -> ( Model, Cmd Msg ) , view : Model -> Html Msg } But `application` needs the 1st argument to be: { init : () -> Url -> Navigation.Key -> ( Model, Cmd Msg ) , onUrlChange : Url -> Msg , onUrlRequest : UrlRequest -> Msg , subscriptions : Model -> Sub Msg , update : Msg -> Model -> ( Model, Cmd Msg ) , view : Model -> Document Msg } Detected errors in 1 module. @ ./src/index.js 10:0-33 12:0-3 @ multi ./config/polyfills.js ./scripts/utils/webpackHotDevClient.js ./node_modules/react-error-overlay/lib/index.js ./src/index.js Errors 😒 Browser Application Oh, our View is still returning HTML and we've told it that we are not returning type DOCUMENT. 😭 πŸ€¦β€β™€οΈ Ok. Adding view function to return type Document Browser.Application let's us set the title and body So we are assembling the content we want as a tuple into a record of type Document in our view function using the viewContent function that receives the model.page IT'S WORKING man that is such a good feeling Reimplement in yet another project branching from Meow Notes, the theme 'Create Content', wordpress headless CMS, terrible mobile support for it, and micro.blog / indie web as a platform. The pieces make sense. Create type Route this is a custom type of our possible routes We've got a routes function that has a type signature Parser (Route -> a) -> a this is all Url.Parser as Parser Parser.oneOf Try a bunch of different path parsers. If there are multiple parsers that could succeed, the first one wins. Parser.map Parser.top A parser that does not consume any path segments. Parser.s Parse a segment of the path if it matches a given string. It is almost always used with </> or oneOf . For example: string Parse a segment of the path as a String . (</>) Parse a path with multiple segments. Docs and Package Info Picks What is a URL? A URL is defined by Tim Berners-Lee in this document . It is worth reading, but I will try to share some highlights. He shares an example like this: \___/ \______________/\_________/ \_________/ \__/ | | | | | scheme authority path query fragment Resources Elm Conf CFP Elm Programming React Router Docs Follow JavaScript to Elm Twitter: @jstoelm Email: jesse@jstoelm.com Jesse Tomchak Twitter: @jtomchak

Where can I listen to Hello Darkness My Old Friend?

You can listen to Hello Darkness My Old Friend online on Radio and Podcast. Open the player on this page to stream the available audio.

Which podcast is Hello Darkness My Old Friend from?

Hello Darkness My Old Friend is an episode from Javascript to Elm by Jesse Tomchak.

How long is this episode?

This episode is 25:01 long.

When was this episode published?

This episode was published on Apr 18, 2019.

Can I save Hello Darkness My Old Friend for later?

Yes. Use the heart button on the episode page to add it to your favorite episodes list.

Are there related episodes from Javascript to Elm?

Yes. This page shows related episodes from Javascript to Elm when more episodes are available from the podcast feed.

Quick Answers About This Episode

Where can I listen to Hello Darkness My Old Friend?

You can listen to Hello Darkness My Old Friend on this page when the episode audio is available from the podcast feed.

Which podcast is this episode from?

Hello Darkness My Old Friend is from Javascript to Elm by Jesse Tomchak.

What are the episode details?

Published Apr 18, 2019 and 25:01 long