Mithril JavaScript Tutorial

Mithril JavaScript Tutorial

This article take a fresh look at a Mithril JavaScript Tutorial. Recently we were taking a look at the very slick Flarum Forum Software here at Vegibit, and one of the things we discovered is that it makes use of a great high performance JavaScript framework called Mithril. Peaking our interest, we decided to read up more about Mithril and have a play with the features it offers ourselves. What we found was an incredibly small footprint, and blazing performance, all the while staying true to a mostly native JavaScript syntax. Mithril Js is really cool, let’s learn all about it now.


Creating Virtual DOM Elements

We’ll begin by creating a button in the Virtual DOM, and then rendering it.

<div id="mfunction"></div>


First we used the m() function to create a virtual anchor tag with bootstrap classes assigned. This was assigned to the variable renderthis. Next, we grab the element on the page with the id of one, and assign that to the variable attachto. Finally, we call the m.render() function and pass to it the attach point, and the newly created element to render. Cool stuff!


Mithril Components and Views

<div id="basiccomponentone"></div>


A more common way to display elements on the page is by using components. To do this requires a few steps.

  1. Create a JavaScript Object
  2. Assign a function to the view property of said object
  3. Mithril will render what the function stored in view returns

Following these steps, we create a JavaScript object named Button, store a function in it’s view property, then pass the object to m.mount as the second argument. The first argument is where we want to attach it in the DOM.

Let’s look at another way to create a component, with a Hello World type example. This time around, we will say “Hello Mithril” using Mithril of course.

<div id="basiccomponenttwo"></div>


Changing Application State

<div id="applicationstate"></div>


In this example, we start to look at the ability to change state in JavaScript, and have our views be updated in real time. If you click the button, you will see that each time you click it, the count increments. How does this happen? The key is writing views that reflect the state of your application.

The function contained in view will be run anytime an event which has been created in that function gets fired. It works kind of like this:

  1. The page loads, and JavaScript executes, displaying the button.
  2. The user clicks the button, which invokes onclick, which in turn causes the function stored in view to run again!
  3. When that function runs, the virtual DOM is evaluated. If there are changes since the last iteration, the real DOM get’s updated.

Two Way Data Binding

Libraries like Angular, React, and VueJS introduced the concept of two way data binding. It is one of the core reasons in fact to even use a view library like this. You change the state of the application, and the view updates in real time, almost as if by magic. You can do two way data binding with Mithril as well, but the approach is slightly different. You will absolutely need to read up on m.prop() and m.withAttr() as these two functions are used together to make the data binding work. Let’s show a quick example, then run through the code to make sure we understand it.

<div id="twowaydatabinding"></div>

Did you try typing some text into the text box? Cool, right?! How does it work?

Well, the official documentation tells us that in Mithril, a component is simply an object which has both a controller and view function. So it looks like we meet the requirements of having a component since we have an app object, which holds a function in both it’s controller and view properties. As you type in the text field, a property called stuffwetype gets updated. This is due to the getter / setter functionality of m.prop(). The changes that occur to this property trigger visual updates in the View. Now comes the funky part, m.withAttr(). m.withAttr() is a little tricky to grasp at first, but we’ll step through it here. First off, the official Mithril documentation tells us the following:

The m.withAttr utility is a functional programming tool provided by Mithril to minimize the need for anonymous functions in the view.

m.withAttr() takes two arguments.

The first is a string representation of the element in the DOM to fetch. Note that m.withAttr() occurs inside of the call to m() which renders an input element. We are passing the string value of value to m.withAttr(). In this context, we are saying we want the current DOM value of the value property of the input element. Now, here is the tricky part.

The second argument passed to the m.withAttr() function is itself a callback. In other words, it is a function that will run, and here is the kicker: It is passed the property value of said element into it upon execution. In our case, we can see that the callback (argument two of m.withAttr()) is input.stuffwetype. As it turns out, what is stored in input.stuffwetype is the m.prop() getter / setter function which we declared in the controller. So this is what we mean by m.prop() and m.withAttr() working together. In an indirect kind of way, m.prop() is usually the second argument passed to m.withAttr(). Don’t you just love functional programming?! 🙂


withAttr Example

We’ll look at another example just to be sure we see how this special function works.

In this example, the second argument to m.withAttr() is this.showoutput just for fun. We know that the button on the page has a class of btn-lg, since this is how we constructed our view. Now, we can almost read the code of the call to m() in that view function like so: Go get me the value of the class property of the button element in the page, then call the function that exists in this.showoutput, and pass the value you found in step one to that function.

We create a simple little function that then renders the result of that operation onto the screen. If all went well, you should see “The button you clicked has the class of btn-lg” when you actually clicked the button. Smashing!


m.prop() and m.withAttr() work together

Let’s look at another example of how the m.prop() and m.withAttr() functions work together. In the text box below, type a message to send as an alert, then click the button. Try it a few times if you like.


High Performance Rendering

This next example is a slightly modified version of some example code provided by Einar Norðfjörð from a rvk.js meetup. This is pretty incredible! His example makes use of a setInterval function in the controller to populate an array of 100 random numbers every 20 milliseconds. For brevity, in our example we just bump it down to 25. In the example below, you can click the run button to make it active and the stop button to make it stop. Now what is incredible, is that this code snippet does not actually need to remove the DOM and create a new DOM. It *only* replaces the value inside of the td element. This type of rendering is why Mithril is so incredibly fast. You’ll note your browser does not even break a sweat during execution of this example code.


Views Are JavaScript

Before we go to much further, we will discuss a little bit about how views are created in Mithril. You may have noticed that we have not written any actual HTML in our code examples. So where the heck is the view? We have been trained recently to adhere to the new approach of pseudo HTML / JavaScript proprietary markup made popular by frameworks like Angular and React. In Mithril, there is no such need to strictly adhere to this approach, though you can make use of it if you like with the MSX HTML syntax preprocessor.

With this knowledge, we can see that Views in Mithril are created by using the m() utility method to create virtual DOM elements, which are then attached to the page with something like m.mount(). Recall that the convention is to have a function stored in the view property which should return the desired markup to appear on the actual page. It is a little different than what you might be used to, but it is very efficient and fast. A huge benefit provided by Leo Horie, the creator of Mithril, is a handy HTML to Mithril View converter. Simply paste in your HTML, and the Mithril specific JavaScript will be output to you. This makes working with this approach much easier, especially early on when you are coming up to speed with how to best make use of the m() utility function.


Putting It All Together: Task MVC

The final piece to our Mithril JavaScript Tutorial will be to hammer out a slightly refactored version of the sample application provided by the official documentation. We'll see that most of it is native JavaScript, with sprinklings of calls to the Mithril API, and an application structure which follows best practices with regard to Models, Views, Controllers, and Components. A task object will namespace the entire application, while a native JavaScript constructor sets up the properties of taskdescription and isfinished. The TaskList class holds all of the tasks for completion. Within our task object is a viewmodel property which holds the function representing the view model. It uses private variables and logic to track all tasks, store descriptions, and clears the text input field as needed. The task object also has a controller property which holds the function which simply initializes the viewmodel. Finally, we have the view property which holds a function that returns the markup to be rendered on page. Of course if makes heavy use of the m() utility function. If you choose to work with Mithril, this is one function you will rapidly become familiar with! Lastly, the application is attached to the DOM, and it is ready for action.


Mithril JavaScript Tutorial Summary

Mithril JS is The High Performance JavaScript Framework! It's size and speed make it a fine choice for your view model needs. The thing about Mithril is that it does not try to force you into a hybrid syntax heavy architecture, which may be subject to change radically over time. Most of working in Mithril is plain JavaScript. In addition to that, Mithril is a gateway drug to true functional programming. It nudges you in that direction, and certainly helps you keep your native JavaScript chops growing. All of this makes Mithril JS a framework to dive into and learn from.