Chapter 15 – Navigation & URL Parser

Tram Ho

In this article, we will look at a simple example that Elm gives of handling the request to navigate between single content pages when the user clicks on any link in SPA. To implement this example, we will need to install an additional package support.

Navigation

The first is still the main program with full architectural elements required as input parameters of Browser.application< /code>.

We’ll have a very simple HTML architecture consisting of a list of <ul> navigation links and a <div> displays the content of the single page associated with each link. The content displayed here will simply be the full path of the single page to be displayed. Therefore, init and view should also be quite simple.

Here neither init and view have no point sending Msg</ code> to the main controller Program. However, there is a new element that is the Key style used at init. This value is used to ensure that the sub-program of module Navigation includes pushUrl, replaceUrl, back, and forward will indicate valid if supplied correctly key initialized when the new web page was downloaded to the browser.

The cases handled at update will now include:

  • User enters a new address in the Address Bar address bar of the web browser.
  • User clicks on a link in the web page:
    • Link to a single site in the same system
    • Link pointing to another website

And the rest of the architectural elements…

SPA’s navigation logic is pretty easy to follow now with onUrlChange and onUrlRequest. Because after all, when the association changes, we only have these possibilities:

  • Or user enters a new address in address bar, then onUrlChange will be fired and UrlChanged is sent to update.
  • Or the user clicks on a link in the web page, onUrlRequest will be fired and LinkClicked will be activated. send to update.

http://localhost:8000/src/ Main.elm

Only case where update needs to generate Model records with url The new is when a user clicks on a link that points to a single page of content in the same system. That’s when the new Model record is sent to view and we will have the look and feel of our website changed to the new interface. single page appearance that meets the requirements.

And of course, in this case the web browser won’t need to reload the entire HTML text and the user won’t have to temporarily see a blank window.

URL Parser

In fact, after basic navigation, we will have to learn more about how to parse the request path Request Url in the case of a user click on a link that points to a single page in the same system.

However, learning the tools to handle this task group will require the use of sub-program called Higher Order Functions that I have determined is for the next Sub-Series. And so our SPA story will need to go back a bit until Higher Order Functions and some related concepts are finished in the new Sub-Series :

(unpublished) [Functional Programming + Elm/Haskell] Lesson 1 – …

In parallel, the Sub-Series Declarative will still be continued as a small project building a SPA using the Back-End is Wikipedia API.

(not published) [Declarative Programming + Elm] Lesson 16 – …

Share the news now

Source : Viblo