Skip to content

Commit

Permalink
Lots of changes to the examples, these should work seamlessly in a tu…
Browse files Browse the repository at this point in the history
…torial now
  • Loading branch information
Techwraith committed Mar 13, 2012
1 parent 887ea26 commit f01c2d5
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 103 deletions.
27 changes: 22 additions & 5 deletions examples/todo_app/app/controllers/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand Down
61 changes: 38 additions & 23 deletions examples/todo_app/app/views/todos/_form.html.ejs
Original file line number Diff line number Diff line change
@@ -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>

1 change: 0 additions & 1 deletion examples/todo_app/app/views/todos/add.html.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<div class="hero-unit">
<h2>Add a ToDo:</h2>
<%= partial('_form', {params: params}); %>
</div>
1 change: 0 additions & 1 deletion examples/todo_app/app/views/todos/edit.html.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<div class="hero-unit">
<h2>Update ToDo:</h2>
<%= partial('_form', {params: params, todo: todo}); %>
</div>
2 changes: 1 addition & 1 deletion examples/todo_app/app/views/todos/index.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions examples/todo_app/config/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

geddy.todos = [];

// Add uncaught-exception handler in prod-like environments
if (geddy.config.environment != 'development') {
process.addListener('uncaughtException', function (err) {
Expand All @@ -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 = [];

37 changes: 34 additions & 3 deletions examples/todo_app/lib/model_adapters/todo.js
Original file line number Diff line number Diff line change
@@ -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"});

}

})();
Expand Down
57 changes: 39 additions & 18 deletions examples/todo_app_mongo/app/controllers/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
12 changes: 1 addition & 11 deletions examples/todo_app_mongo/app/views/todos/add.html.ejs
Original file line number Diff line number Diff line change
@@ -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>
7 changes: 1 addition & 6 deletions examples/todo_app_mongo/app/views/todos/edit.html.ejs
Original file line number Diff line number Diff line change
@@ -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>
2 changes: 1 addition & 1 deletion examples/todo_app_mongo/app/views/todos/index.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
<% } %>
Expand Down
30 changes: 5 additions & 25 deletions examples/todo_app_mongo/app/views/todos/show.html.ejs
Original file line number Diff line number Diff line change
@@ -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>
Loading

0 comments on commit f01c2d5

Please sign in to comment.