Loading Page

by osban

Level: beginner • Mithril.js Version: latest

Here we see an example showing a loading page component while loading external data. For this we are using the conditional ternary operator. In Mithril.js the ternary operator is one of the often seen operators in view methods.

Note: You have to click the refresh button in Flems to see the loading message.

Live Example

Dependencies

Type Name URL
scriptmithril@latesthttps://unpkg.com/mithril@latest

JavaScript

const model = {
  data: []
}

const Loading = {
  view: () => m('h1', 'Loading...')
}

const Main = {
  oninit: () => {
    // fake fetching data here
    setTimeout(() => {
      model.data = 'The page data';
      m.redraw()
    }, 2000)
  },
  view: () => [
    !model.data.length
      ? m(Loading)
      : [
        m('h1', 'The page'),
        m('div', model.data)
      ]
  ]
}

m.mount(document.body, Main)

As a prerequisite for this snippet, the latest version of Mithril.js framework is required. Beginners should have no problems following this example, that simply shows some basic recipies.

In this code sample Mithril.js' m.mount API method is use, besides the basic hyperscript function m(). Moreover, it shows how the lifecycle method oninit can be used (better known as hook).

The example was written by osban, last edits were made on 24 September 2020. The author has contributed some more snippets. Click here to see them all.

Contribute

Did you note a typo or something else? So let me know by opening an issue. Or much better: just fork the repository on GitHub, push your commits and send a pull request. Ready to start your work? Then click on the edit link below. Thanks in advance!

See more code examples  •  Edit this example on GitHub