Making a To-Do List With flatiron.js (Story 4)
This post continues the creation of a to-do list with flatiron.js.
Previous Stories in this Series
Now, we need to go back and change the handler for GET to list the tasks found in the session that we added back in the story 3.
Now, we’ll use plates to build the list of items and, then, put it back
into the overall page. plates binds values based on HTML attributes such
as the id
or class
of the elements in the template. This removes the
gunk that you normally find in templates. I like that idea, a lot. However, it
does not handle collections of values, so we need to do that ourselves.
First, we need to include the plates library into the script.
1 | // server.js |
I added a UL tag to my index.html
with an id of “task-list”. Now, in the
server.js
file, in the GET handler for /, we need to render the tasks
into LI tags. Here’s what mine looks like.
1 | app.router.get('/', function() { |
I run it and everything seems fine except when I click the “Add” button, no new task appears in my list. No empty LI tag. No nothing. What’s going on? It’s like the session doesn’t remember my tasks between requests. So, I’m going to put a log statement in the check for my task list being undefined.
1 | if(typeof self.req.session.tasks === 'undefined') { |
When I run it, sure enough, after a post, I see that message printed. Darn it. The connect session plugin must not be working. Once again, going to the source of session.js, I notice that the plugin writes the session cookie when the Response object emits a ‘header’ event. That could be it. So, I’ll put that into my POST handler because I want the cookie written at that point.
1 | app.router.post('/', function() { |
I type npm start
and after I POST a new to-do item … BOOM!
1 | TypeError: Cannot read property 'encrypted' of undefined |
Back to the source and I see that the session plugin requires a value
for req.connection.encrypted
. Well, let’s stub that value and see what
happens.
1 | app.router.post('/', function() { |
Again, with the npm start
and … SUCCESS! Now, when I post my task, it
appears in my list! Hooray!