Permalink
Browse files

new todos and redis yay

  • Loading branch information...
jackfranklin committed Jun 24, 2012
1 parent 7a89115 commit c164d7e477128553014885f2925c2f7139d01975
Showing with 32 additions and 17 deletions.
  1. +3 −2 app.js
  2. +3 −1 package.json
  3. +20 −10 routes/routes.js
  4. +6 −4 views/todo.jade
View
5 app.js
@@ -3,8 +3,8 @@
* Module dependencies.
*/
-var express = require('express')
- , routes = require('./routes/routes');
+var express = require('express'),
+ routes = require('./routes/routes');
var app = module.exports = express.createServer();
@@ -32,6 +32,7 @@ app.configure('production', function(){
app.get('/', routes.index);
app.get('/about', routes.about);
app.get('/todo', routes.todo);
+app.post('/save', routes.saveTodo);
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
View
@@ -5,7 +5,9 @@
"dependencies": {
"express": "2.5.8",
"jade": ">= 0.0.1",
- "localStorage": "~1.0.0"
+ "localStorage": "~1.0.0",
+ "redis": "~0.7.2",
+ "connect-redis": "~1.4.0"
},
"scripts": {
"start": "node app.js"
View
@@ -1,8 +1,4 @@
-/*
- * GET home page.
- */
-
exports.index = function(req, res){
res.render('index', { title: 'Welcome to JSPlayground Todo' });
};
@@ -12,16 +8,30 @@ exports.about = function(req, res){
};
exports.todo = function(req, res){
- res.render('todo', {
- title: 'New Todo List',
- todos: [
- { text: "todo number 1" },
- { text: "todo number 2" }
- ]
+ var todos = [];
+ client.hgetall("Todo", function(err, objs) {
+ for(var k in objs) {
+ var newTodo = {
+ text: objs[k]
+ };
+ todos.push(newTodo);
+ }
+ res.render('todo', {
+ title: 'New Todo List',
+ todos: todos
+ });
});
};
+var redis = require("redis"),
+ client = redis.createClient();
+
exports.saveTodo = function(req, res) {
+ var newTodo = {};
+ newTodo.name = req.body['todo-text'];
+ newTodo.id = newTodo.name.replace(" ", "-");
+ client.hset("Todo", newTodo.id, newTodo.name);
+ res.redirect("back");
};
View
@@ -1,9 +1,11 @@
h1 new todo list
-form
+form(action="/save", method="post")
p
label Enter a new todo item
- input(type='text',placeholder='new todo')
+ input(type='text',placeholder='new todo', name='todo-text')
+ p
+ input(type='submit', value='Save')
ul
-each todo in todos
- li #{todo.text}
+ each todo in todos
+ li #{todo.text}

0 comments on commit c164d7e

Please sign in to comment.