The flatiron library is no more. So, other than reading stuff, there just isn't all that much relevant stuff, here.

This post continues the creation of a to-do list with flatiron.js.

Previous Stories in this Series

Oooh, a model. Finally! Let’s use resourceful to define this ToDo model.

Turns out that the most recently released version of resourceful suffers from some bugs, too. So, modify your package.json file to read

1
2
3
4
5
6
7
8
9
10
11
{
"name": "todo-list",
"version": "0.1.0",
"dependencies": {
"flatiron": "*",
"union": "*",
"plates": "*",
"resourceful": "git://github.com/flatiron/resourceful.git",
"connect": "git://github.com/senchalabs/connect.git"
}
}

and run the commands

1
2
npm uninstall resourceful
npm install

resourceful - A storage agnostic resource-oriented ODM for building prototypical models with validation and sanitization.

That’s quite a mouthful. In short, resourceful allows us to define prototypes that will ensure that the properties have values of the correct type assigned to them and other simple validations.

For us, though, we just want a pretty simple Task class that has an identifier, some text to describe the task, and a created date. I think would fit the bill, pretty well. At the top of our server.js file, we should require resourceful and define our Task prototype.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// server.js

var session = require('connect').session
, cookieParser = require('connect').cookieParser
, flatiron = require('flatiron')
, app = flatiron.app
, fs = require('fs')
, resourceful = require('resourceful')
;

var Task = resourceful.define('task', function() {
this.timestamps();
this.string('desc').required(true)
.minLength(1);
});

Now, down after the definition for GET, I will define my POST logic. I will need to parse the body of the post, so I will include a qs = require('querystring') at the top of the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
app.router.post('/', function() {
var self = this;
var task = new (Task)({desc: self.req.body.desc});
if(task.validate().valid) {
task.save();
if(!self.req.session.tasks) {
self.req.session.tasks = [];
}
self.req.session.tasks.push(task);
}
self.res.writeHead(303, {'Location': '/'});
self.res.end();
});

Not bad at all.