Browse files

Lots of changes to the examples, these should work seamlessly in a tu…

…torial now
  • Loading branch information...
1 parent 887ea26 commit f01c2d528ebbf221827c31c5ff20e239fa431dc8 Techwraith committed Mar 12, 2012
View
27 examples/todo_app/app/controllers/todos.js
@@ -3,7 +3,10 @@ var Todos = function () {
this.respondsWith = ['html', 'json', 'js', 'txt'];
this.index = function (req, resp, params) {
- this.respond({params: params, todos: geddy.todos});
+ var self = this;
+ geddy.model.adapter.Todo.all(function(err, todos){
+ self.respond({params: params, todos: todos});
+ });
};
this.add = function (req, resp, params) {
@@ -30,22 +33,23 @@ var Todos = function () {
this.show = function (req, resp, params) {
var self = this;
- geddy.model.adapter.Todo.load(params.id, function(todo){
+ geddy.model.adapter.Todo.load(params.id, function(err, todo){
self.respond({params: params, todo: todo});
});
};
this.edit = function (req, resp, params) {
var self = this;
- geddy.model.Todo.load(params.id, function(todo){
+ geddy.model.Todo.load(params.id, function(err, todo){
self.respond({params: params, todo: todo});
});
};
this.update = function (req, resp, params) {
var self = this;
- geddy.model.adapter.Todo.load(params.id, function (todo) {
- todo.status = params.status ? 'done' : 'open';
+ geddy.model.adapter.Todo.load(params.id, function (err, todo) {
+ todo.status = params.status;
+ todo.title = params.title;
todo.save(function (err, data) {
if (err) {
params.errors = err;
@@ -58,6 +62,19 @@ var Todos = function () {
});
};
+ this.remove = function (req, resp, params) {
+ var self = this;
+ geddy.model.adapter.Todo.remove(params.id, function(err){
+ if (err) {
+ params.errors = err;
+ self.transfer('edit');
+ }
+ else {
+ self.redirect({controller: self.name});
+ }
+ });
+ }
+
};
exports.Todos = Todos;
View
61 examples/todo_app/app/views/todos/_form.html.ejs
@@ -1,31 +1,46 @@
<%
var isUpdate = params.action == 'edit'
- , action = isUpdate ? '/todos/' + todo.id + '?_method=PUT': '/todos'
+ , formTitle = isUpdate ? 'Update this To Do Item' : 'Create a new To Do Item'
+ , action = isUpdate ? '/todos/' + todo.id + '?_method=PUT' : '/todos'
+ , deleteAction = isUpdate ? '/todos/' + todo.id + '?_method=DELETE' : ''
, btnText = isUpdate ? 'Update' : 'Add'
, doneStatus = isUpdate ? 'checked' : ''
+ , titleValue = isUpdate ? todo.title : ''
, errors = params.errors;
%>
- <form id="todo-form" action="<%= action %>" method="POST">
- <% if (errors) {
- for (var p in errors) { %>
- <div><%= errors[p]; %></div>
- <% }
- }
- %>
- <div>
- <% if (isUpdate) { %>
- <%= todo.title %>
- <input type="hidden" class="span6" placeholder="<%= todo.title %>" name="title"/>
- <% }
- else {%>
- <input type="text" class="span6" placeholder="enter title" name="title"/>
- <% } %>
- </div>
- <div>
- <input type="checkbox" name="status" <%= doneStatus %> /> Done
- </div>
- <div>
- <input type="submit" class="btn btn-primary" value="<%= btnText %>">
- </div>
+ <form id="todo-form" class="form-horizontal" action="<%= action %>" method="POST">
+ <fieldset>
+ <legend><%= formTitle %></legend>
+ <div class="control-group">
+ <label for="title" class="control-label">Title</label>
+ <div class="controls">
+ <input type="text" class="span6" placeholder="enter title" name="title" value='<%= titleValue %>'/>
+ <% if (errors) { %>
+ <p>
+ <% for (var p in errors) { %>
+ <div><%= errors[p]; %></div>
+ <% } %>
+ </p>
+ <% } %>
+ </div>
+ </div>
+ <% if (isUpdate) { %>
+ <div class="control-group">
+ <label for="status">Status</label>
+ <div class="controls">
+ <select name="status">
+ <option>open</option>
+ <option>done</option>
+ </select>
+ </div>
+ </div>
+ <% } %>
+ <div class="form-actions">
+ <input type="submit" class="btn btn-primary" value="<%= btnText %>"/>
+ <% if (isUpdate) { %>
+ <button type="submit" formaction="<%= deleteAction %>" formmethod="POST" class="btn btn-danger">Remove</button>
+ <% } %>
+ </div>
+ </fieldset>
</form>
View
1 examples/todo_app/app/views/todos/add.html.ejs
@@ -1,4 +1,3 @@
<div class="hero-unit">
- <h2>Add a ToDo:</h2>
<%= partial('_form', {params: params}); %>
</div>
View
1 examples/todo_app/app/views/todos/edit.html.ejs
@@ -1,4 +1,3 @@
<div class="hero-unit">
- <h2>Update ToDo:</h2>
<%= partial('_form', {params: params, todo: todo}); %>
</div>
View
2 examples/todo_app/app/views/todos/index.html.ejs
@@ -2,7 +2,7 @@
<h2>To Do List</h2>
<a href="/todos/add" class="btn pull-right">Create a new To Do</a></p>
</div>
-<% if (todos.length) { %>
+<% if (todos && todos.length) { %>
<% for (var i in todos) { %>
<div class="row todo-item">
<div class="span8"><h3><a href="/todos/<%= todos[i].id; %>/edit"><%= todos[i].title; %></a></h3></div>
View
4 examples/todo_app/config/init.js
@@ -1,4 +1,6 @@
+geddy.todos = [];
+
// Add uncaught-exception handler in prod-like environments
if (geddy.config.environment != 'development') {
process.addListener('uncaughtException', function (err) {
@@ -8,5 +10,3 @@ if (geddy.config.environment != 'development') {
geddy.model.adapter = {};
geddy.model.adapter.Todo = require(process.cwd() + '/lib/model_adapters/todo').Todo;
-geddy.todos = [];
-
View
37 examples/todo_app/lib/model_adapters/todo.js
@@ -1,28 +1,59 @@
var Todo = new (function () {
+ this.all = function (callback) {
+ callback(null, geddy.todos);
+ }
+
this.load = function (id, callback) {
+
for (var i in geddy.todos) {
if (geddy.todos[i].id == id) {
- callback(geddy.todos[i]);
+ callback(null, geddy.todos[i]);
return;
}
}
- callback({});
+ callback({message: "To Do not found"}, null);
+
};
this.save = function (todo, opts, callback) {
+
+ if (typeof callback != 'function') {
+ callback = function(){};
+ }
+
+ var todoErrors = null;
for (var i in geddy.todos) {
// if it's already there, save it
if (geddy.todos[i].id == todo.id) {
geddy.todos[i] = todo;
- return callback(null, todo);
+ todoErrors = geddy.model.Todo.create(todo).errors;
+ return callback(todoErrors, todo);
}
}
todo.saved = true;
geddy.todos.push(todo);
return callback(null, todo);
+
+ }
+
+ this.remove = function(id, callback) {
+
+ if (typeof callback != 'function') {
+ callback = function(){};
+ }
+
+ for (var i in geddy.todos) {
+ if (geddy.todos[i].id == id) {
+ geddy.todos.splice(i, 1);
+ return callback(null);
+ }
+ }
+
+ return callback({message: "To Do not found"});
+
}
})();
View
57 examples/todo_app_mongo/app/controllers/todos.js
@@ -14,16 +14,21 @@ var Todos = function () {
};
this.create = function (req, resp, params) {
- var self = this;
- var todo = geddy.model.Todo.create({title: params.title, id: geddy.string.uuid(10), status: 'open'});
- if (todo.isValid()) {
- todo.saved = true;
- todo.save(function(){
- self.redirect({controller: this.name});
- });
- } else {
- this.redirect({controller: this.name, action: 'add?error=true'});
- }
+ var self = this
+ , todo = geddy.model.Todo.create({
+ title: params.title
+ , id: geddy.string.uuid(10)
+ , status: 'open'
+ });
+ todo.save(function (err, data) {
+ if (err) {
+ params.errors = err;
+ self.transfer('add');
+ }
+ else {
+ self.redirect({controller: self.name});
+ }
+ });
};
this.show = function (req, resp, params) {
@@ -36,24 +41,40 @@ var Todos = function () {
this.edit = function (req, resp, params) {
var self = this;
geddy.model.Todo.load(params.id, function(err, todo){
- if (todo && !err) {
- self.respond({params: params, todo: todo});
- } else {
- resp.end();
- }
+ self.respond({params: params, todo: todo});
});
};
this.update = function (req, resp, params) {
var self = this;
- geddy.model.adapter.Todo.load(params.id, function(err, todo){
+ geddy.model.adapter.Todo.load(params.id, function (err, todo) {
todo.status = params.status;
- todo.save(function(){
- self.redirect({controller: this.name});
+ todo.title = params.title;
+ todo.save(function (err, data) {
+ if (err) {
+ params.errors = err;
+ self.transfer('edit');
+ }
+ else {
+ self.redirect({controller: self.name});
+ }
});
});
};
+ this.remove = function (req, resp, params) {
+ var self = this;
+ geddy.model.adapter.Todo.remove(params.id, function(err){
+ if (err) {
+ params.errors = err;
+ self.transfer('edit');
+ }
+ else {
+ self.redirect({controller: self.name});
+ }
+ });
+ }
+
};
exports.Todos = Todos;
View
12 examples/todo_app_mongo/app/views/todos/add.html.ejs
@@ -1,13 +1,3 @@
<div class="hero-unit">
- <h2>Add a ToDo:</h2>
- <form action="/todos" method="POST">
- <% if (params.error) {
- var title = 'title not long enough, must be 5 characters or more.'
- } else {
- var title = 'enter title'
- }
- %>
- <input type="text" class="span6" placeholder="<%= title %>" name="title"/>
- <input type="submit" class="btn btn-primary">
- </form>
+ <%= partial('_form', {params: params}); %>
</div>
View
7 examples/todo_app_mongo/app/views/todos/edit.html.ejs
@@ -1,8 +1,3 @@
<div class="hero-unit">
- <h3>Params</h3>
- <ul>
- <% for (var p in params) { %>
- <li><%= p + ': ' + params[p]; %></li>
- <% } %>
- </ul>
+ <%= partial('_form', {params: params, todo: todo}); %>
</div>
View
2 examples/todo_app_mongo/app/views/todos/index.html.ejs
@@ -5,7 +5,7 @@
<% if (todos && todos.length) { %>
<% for (var i in todos) { %>
<div class="row todo-item">
- <div class="span8"><h3><a href="/todos/<%= todos[i].id; %>"><%= todos[i].title; %></a></h3></div>
+ <div class="span8"><h3><a href="/todos/<%= todos[i].id; %>/edit"><%= todos[i].title; %></a></h3></div>
<div class="span4"><h3><i class="icon-list-alt"></i><%= todos[i].status; %></h3></div>
</div>
<% } %>
View
30 examples/todo_app_mongo/app/views/todos/show.html.ejs
@@ -1,28 +1,8 @@
<div class="hero-unit">
- <h3><%= todo.title; %></h3>
- <div class="pull-right">
- <% if (todo.status == 'open') { %>
- <form id="finish-todo" class="hidden" action="/todos/<%= todo.id;%>">
- <input type="hidden" name="status" value="done"/>
- <input type="hidden" name="id" value="<%= todo.id; %>"/>
- <input type="hidden" name="title" value="<%= todo.title; %>">
- </form>
- <span><a href="#" class="btn btn-primary btn-large" id="finish-btn">Finish To Do</a></span>
- <script type="text/javascript">
- var form = $('#finish-todo');
- $('#finish-btn').click(function(e){
- e.preventDefault();
- $.ajax({
- type: "PUT",
- url: form.attr('action'),
- data: form.serialize()
- }).done(function( msg ) {
- $(e.target).replaceWith('<p>This todo is finished!</p>');
- });
- })
- </script>
- <% } else { %>
- <p>This to do is finished!</p>
+ <h3>Params</h3>
+ <ul>
+ <% for (var p in params) { %>
+ <li><%= p + ': ' + params[p]; %></li>
<% } %>
- </div>
+ </ul>
</div>
View
28 examples/todo_app_mongo/lib/model_adapters/todo.js
@@ -41,39 +41,45 @@ var Todo = new (function () {
};
- this.save = function (todo, callback) {
+ this.save = function (todo, opts, callback) {
// sometimes we won't need to pass a callback
if (typeof callback != 'function') {
callback = function(){};
}
// Mongo doesn't like it when you send functions to it
// so lets make sure we're only using the properties
- todo = {
+ cleanTodo = {
id: todo.id
, saved: todo.saved
, title: todo.title
, status: todo.status
};
+ // Double check to see if this thing is valid
+ todo = geddy.model.Todo.create(cleanTodo);
+
+ if (!todo.isValid()) {
+ return callback(todo.errors, null);
+ }
+
// Check to see if we have this to do item already
geddy.db.todos.findOne({id: todo.id}, function(err, doc){
- // if theres an error, log it and
if (err) {
- geddy.log.error(err);
return callback(err, null);
}
// if we already have the to do item, update it with the new values
if (doc) {
- geddy.db.todos.update({id: todo.id}, todo, function(err, docs){
- return callback(err, docs);
+ geddy.db.todos.update({id: todo.id}, cleanTodo, function(err, docs){
+ return callback(todo.errors, todo);
});
}
// if we don't already have the to do item, save a new one
else {
+ todo.saved = true;
geddy.db.todos.save(todo, function(err, docs){
return callback(err, docs);
});
@@ -83,6 +89,16 @@ var Todo = new (function () {
}
+ this.remove = function(id, callback) {
+ if (typeof callback != 'function') {
+ callback = function(){};
+ }
+
+ geddy.db.todos.remove({id: id}, function(err, res){
+ callback(err);
+ });
+ }
+
})();
exports.Todo = Todo;

0 comments on commit f01c2d5

Please sign in to comment.