While attempting to copy and improve upon the todo list in the front page of canjs.org I have run into a bit of a snag. The example doesn't show you how to add items to a todo list. So I added a "Add another" span that executes newTodo when clicked.
<h2>Todays to-dos</h2>
{{#selectedTodo}}
<input type="text" can-value="description" can-change="saveTodo">
{{/selectedTodo}}
<ul>
{{#each todos}}
<li>
<input type="checkbox" can-value="complete" can-click="saveTodo">
<span class="description {{#if complete}}done{{/if}}" can-click="select">{{description}}</span>
<button can-click="destroy"><i class="fa fa-close"></i></button>
</li>
{{/each}}
<li>
<span class="description" can-click="newTodo">Add Another</span>
</li>
</ul>
Next I add the newTodo function which resets the list of Todos after saving the new one.
var Todo = can.Model.extend({
findAll: 'GET /todo/',
findOne: 'GET /todo/{id}/',
update: 'POST /todo/{id}/',
destroy: 'POST /todo/delete/{id}/',
create: 'POST /todo/new/',
}, {});
can.Component.extend({
tag: 'todolist',
template: can.view("/static/can/todo_list.html"),
scope: {
selectedTodo: null,
todos: new Todo.List({}),
select: function(todo){
this.attr('selectedTodo', todo);
},
saveTodo: function(todo) {
todo.save();
this.removeAttr('selectedTodo');
},
newTodo: function() {
var that = this;
var t = new Todo({}).save(function() { that.attr("todos",new Todo.List({})) });
},
}
})
$(function() {
$("#todo-wrapper").append(can.mustache("<todolist></todolist>")({}));
});
However, this causes the list to be wiped entirely and then rewritten, causing an ugly blinking effect. I feel like there's a better way to do this.