Permalink
Browse files

Step 7: refactor out todos for dependency injection

  • Loading branch information...
vkarpov15 committed Jul 25, 2013
1 parent c1511cb commit 67859d39e5f18e2b4762abc39064349c931e9c7e
Showing with 26 additions and 20 deletions.
  1. +18 −1 app.js
  2. +8 −19 routes/index.js
View
19 app.js
@@ -30,9 +30,26 @@ if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
var todos = [
{ description : "Buy eggs",
due : new Date(new Date().getTime() + 24 * 60 * 60 * 1000), // 1 day from now
done : false
},
{ description : "Write next blog post",
due : new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000), // 1 week from now
done : false
},
{ description : "Build todo list app",
due : new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000), // 1 week from now
done : true
},
];
app.get('/', routes.index(todos));
app.get('/users', user.list);
app.post('/todo.json', routes.addTodo(todos));
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
View
@@ -3,29 +3,18 @@
* GET home page.
*/
exports.index = function(req, res){
res.render('index', {
title: 'Express',
todos : [
{ description : "Buy eggs",
due : new Date(new Date().getTime() + 24 * 60 * 60 * 1000), // 1 day from now
done : false
},
{ description : "Write next blog post",
due : new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000), // 1 week from now
done : false
},
{ description : "Build todo list app",
due : new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000), // 1 week from now
done : true
},
]
});
exports.index = function(todos) {
return function(req, res){
res.render('index', {
title: 'Express',
todos : todos
});
};
};
exports.addTodo = function(todos) {
return function(req, res) {
todos.push(req.body);
res.json({ todos : todos });
}
};
};

0 comments on commit 67859d3

Please sign in to comment.