Categories: Codepen
Tags: Sass, JavaScript, jQuery-ui, Mustache
I created a simple to-do list application which allows creating new tasks, checking off and deleting completed tasks, and re-organizing the list any way the user would like.
I used Mustache for templating and rendering the data, and jQueryUI provided an API for user manipulation of the list.
I used Sass to store some color variables, and getting the hamburger buttons right was all CSS:
Using “Montserrat” as the font gave the list a nice look, too.
I began with a few tasks pre-loaded in my data
variable, $template
and $target
jQuery object variables for Mustache to render, and an itemIndex
variable for
keeping track of the current item.
First I needed a way for Mustache to render the content in my data
variable, which
was simple enough. I created a template in the HTML like so:
Then, Mustache can take the data inside the data
variable and render the output:
Super nice and simple! Now that I have the data in my variable displayed on the page,
the next step is to add a function allowing me to add additional tasks. I retrieve
the value of the input field (<input id="list-adder" type="text"/>
):
I didn’t have to add the new item to the DOM directly in my function, I just let
render
handle that!
Templating engines like Mustache.js really help when you want to abstract away something
like iterating through some data and displaying it. What I had to keep clear in my mind
when working on this app, though, was that the DOM and the JavaScript data
variable
are two separate entities. I always made sure that data
contained the correct information
whenever an action occurred (like updating a list item). Then, it’s easy to simply call
the render()
method and update the DOM.
See the Pen [GetItDone](https://codepen.io/acrenwelge/pen/bgroGJ/) by Andrew ([@acrenwelge](https://codepen.io/acrenwelge)) on [CodePen](https://codepen.io).