Permalink
Browse files

Updates to the example To-Do list app to use shared partial for the f…

…orm.
  • Loading branch information...
mde committed Mar 10, 2012
1 parent 18496f7 commit 7af9d719538aa272a9c8f06727262b4c2874eed4
@@ -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) {
@@ -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});
+ }
+ });
});
};
@@ -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>
@@ -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>
@@ -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>
<% } %>
@@ -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>
@@ -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);
}
})();
View
@@ -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);
}
@@ -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);

0 comments on commit 7af9d71

Please sign in to comment.