Skip to content

Commit

Permalink
Updates to the example To-Do list app to use shared partial for the f…
Browse files Browse the repository at this point in the history
…orm.
  • Loading branch information
mde committed Mar 10, 2012
1 parent 18496f7 commit 7af9d71
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 60 deletions.
39 changes: 25 additions & 14 deletions examples/todo_app/app/controllers/todos.js
Expand Up @@ -11,17 +11,21 @@ var Todos = function () {
};

this.create = function (req, resp, params) {
var todo = geddy.model.Todo.create({
title: params.title
, id: geddy.string.uuid(10)
, status: 'open'
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});
}
});
if (todo.isValid()) {
todo.save();
this.redirect({controller: this.name});
} else {
this.redirect({controller: this.name, action: 'add?error=true'});
}
};

this.show = function (req, resp, params) {
Expand All @@ -40,10 +44,17 @@ var Todos = function () {

this.update = function (req, resp, params) {
var self = this;
geddy.model.adapter.Todo.load(params.id, function(todo){
todo.status = params.status;
todo.save();
self.redirect({controller: this.name, id: params.id});
geddy.model.adapter.Todo.load(params.id, function (todo) {
todo.status = params.status ? 'done' : 'open';
todo.save(function (err, data) {
if (err) {
params.errors = err;
self.transfer('edit');
}
else {
self.redirect({controller: self.name});
}
});
});
};

Expand Down
11 changes: 1 addition & 10 deletions examples/todo_app/app/views/todos/add.html.ejs
@@ -1,13 +1,4 @@
<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>
8 changes: 2 additions & 6 deletions examples/todo_app/app/views/todos/edit.html.ejs
@@ -1,8 +1,4 @@
<div class="hero-unit">
<h3>Params</h3>
<ul>
<% for (var p in params) { %>
<li><%= p + ': ' + params[p]; %></li>
<% } %>
</ul>
<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
Expand Up @@ -5,7 +5,7 @@
<% if (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/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>
5 changes: 3 additions & 2 deletions examples/todo_app/lib/model_adapters/todo.js
Expand Up @@ -10,18 +10,19 @@ var Todo = new (function () {
callback({});
};

this.save = function (todo, callback) {
this.save = function (todo, opts, callback) {
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
return callback(null, todo);
}

}
todo.saved = true;
geddy.todos.push(todo);
return callback(null, todo);
}

})();
Expand Down
29 changes: 27 additions & 2 deletions lib/model/index.js
Expand Up @@ -69,7 +69,19 @@ utils.mixin(model, new (function () {
};

// Callback should be in the format of function (err, result) {}
this.save = function (opts, callback) {
this.save = function () {
var args = Array.prototype.slice.call(arguments)
, arg
, opts
, callback;
while ((arg = args.shift())) {
if (typeof arg == 'function') {
callback = arg;
}
else {
opts = arg;
}
}
if (this.errors) {
callback(this.errors, null);
}
Expand All @@ -81,7 +93,20 @@ utils.mixin(model, new (function () {
}
};

this.updateAttributes = function (params, opts, callback) {
this.updateAttributes = function () {
var args = Array.prototype.slice.call(arguments)
, params = args.unshift()
, arg
, opts
, callback;
while ((arg = args.shift())) {
if (typeof arg == 'function') {
callback = arg;
}
else {
opts = arg;
}
}
geddy.model.updateItem(this, params);
if (opts.remote) {
this.save(opts, callback);
Expand Down

0 comments on commit 7af9d71

Please sign in to comment.