Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 962 of the free egghead.io lessons, plus get Elm content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Elm in 7 minutes

6:54 Elm lesson by

We are going to take a high-level look at what an Elm application looks like? We show how to structure apps, as well as, Elm’s elegant syntax by building a small app.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

We are going to take a high-level look at what an Elm application looks like? We show how to structure apps, as well as, Elm’s elegant syntax by building a small app.

Avatar
greg
Html exposing (div, text, button)
import StartApp.Simple as StartApp
import Html.Events exposing (onClick)

main =
    StartApp.start
        {
            model = initialModel,
            view = view,
            update = update
        }

initialModel = 0

type Action
    = Increment
    | Decrement

update action model =
    case action of
        Increment ->
            model + 1
        Decrement ->
            model - 1

view address model =
    div []
        [
            button [onClick address Increment] [text "+"],
            div [] [text (toString model)],
            button [onClick address Decrement] [text "-"]
        ]
$ elm-package install evancz/elm-html
$ elm-package install evancz/start-app
$ elm make Main.elm --output=index.html
Avatar
Bruce

Any plans for a full Elm course? Thanks

In reply to egghead.io
Avatar
Song Yangyu

Thinking would be helpful if a how to setup elm is provided

Avatar
hipertracker

+1 Elm is the most interesting solution for JavaScript fatigue.

In reply to Bruce
Avatar
Nicolas del Valle

A full course on Elm would be great :D

Avatar
Deoward

I agree that an in depth course on Elm would be awesome...

Avatar
aleksandre

Looking forward for more videos, thank you for advocating for such an awesome approach in JS.

Avatar
Thomas

+1 it would be interesting to have a full course on Elm, and on ClojureScript!

Avatar
Zhentian

It would be great can explain why use Elm at the first place? That would be great...

Let's expand this "Hello, World" example into a simple To-do app. We'll follow the Elm architecture and divide our app into three main sections -- a model, update, and view.

We can create a view that has an input box and a button, so we could submit our new to-do. As we could see here, HTML elements are represented by functions that take in two parameters -- a list of attributes and a list of children. Let's call our view "From Main," but first, we need to import the HTML attributes.

Now we can save and refresh our browser. We see our input and our button, but they don't do anything. We need to add a few more things to our app before we can start capturing to-dos.

Let's start by creating a model that will describe our data. We've used type alias to create a model with one property, to-do, and that will be a string. Let's actually create a model value so we have some data when we start our app.

Now that we have a model, what do we do with it? We can move to the update section and create a message that describes what our app can do. Within our message type, we've defined one type of message that our application can respond to.

Now we'll need an update function so we can handle things like "Update text." It will take in a message and our model. Within our case expression, we're looking for "Update text," and now we need to update the model.

What will happen here is Update text will return a new model for us. Since Elm is immutable, we can't actually mutate the model. Before we can call Update text from our view, we need to import Beginner Program.

HTML.beginnerprogram will help us to wire everything together. It takes a record that's looking for three properties -- a model, an update, and a view. We can match it to ours.

We're almost there. What we need to do is now import an event so when we type into the input, it will get updated in the model.

Now we can update our view to dispatch Update text. Now that we're using Beginner Program, it's going to pass a model into our view, and we combine that model to the value of our input.

We can verify everything works. We could add a value into our input and click to-do, but we need to verify that it's working. We could do this by adding a Div that shows us the text as we're typing in.

We have the basic structure in place. Let's expand on our app by updating our model. To-dos will be a list of strings. Now let's add a message called Add Item. Now we can define what we want to happen with that item.

In this case, we want to add whatever is in our input box into our To-dos list. We want our model.todo to go on the front of our model.todo's list. We could do that by using the double colon -- pronounced cons.

We can add an on-click event to our button, and we can't forget to import it. Whatever we have inside of our input when we click the button, it will get added on top of our to-do list, but we're currently not displaying it. Let's do that by creating a to-do list function.

What we want to do is return an unordered list, so we need to map over each item in our to-dos list. Using a Let allows us to keep our work scope locally. I purposely left the first parameter in the map, empty [inaudible 4:58] , because I actually want to create a to-do item.

Finally, let's call to-do list from our view, and pass in to-dos from our model.

Let's verify that's working. If I expand our browser window, we see that we have an error. OM has a great compiler. As you can see here, it gives us the line number and shows us where our error is, and also, it gives us a recommendation how we might want to fix it.

Time to add items to our list. Everything seems to be working, and we can add items to our list, but we're missing one last feature -- how to remove items. Again, we'll follow the same pattern to remove items.

Here, we can use Filter to create a new list that's excluding the to-do we want to remove. We'll create an anonymous function. We start that by using the backslash. Forward slash and equals is the "Not equals" operator.

Finally, let's add a way to remove the item from our view, and let's just pass in the to-do with our Remove Item.

Whoops, I noticed a typo, and I should probably shorten these to "T." Now let's try.

We have our simple To-do app.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?