Skip to content

Instantly share code, notes, and snippets.

@imjoshdean
Created September 20, 2013 19:48
Show Gist options
  • Save imjoshdean/6642864 to your computer and use it in GitHub Desktop.
Save imjoshdean/6642864 to your computer and use it in GitHub Desktop.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://canjs.com/release/latest/can.jquery.js"></script>
<script type="text/javascript" src="http://canjs.com/release/latest/can.fixture.js"></script>
<script type="text/javascript" src="http://canjs.com/release/latest/can.object.js"></script>
</head>
<body>
<div id="todos"></div>
<script id="todoEJS" type="text/ejs">
<ul>
<% todos.each(function(todo) { %>
<li <%= (el) -> el.data('todo', todo); %>>
<%= todo.attr('name'); %>
<input type="checkbox" <%= todo.attr('complete') ? 'checked' : '' %>/>
</li>
<% }); %>
</ul>
</script>
<script>
var Todo = can.Model.extend({
findAll: 'GET /todos',
findOne: 'GET /todos/{id}',
create: 'POST /todos',
update: 'PUT /todos/{id}',
destroy: 'DESTROY /todos/{id}'
}, { });
var TodoList = can.Control.extend({}, {
init: function() {
this.element.html(can.view('todoEJS', {
todos: this.options.todos
}));
},
"input change": function(el, ev) {
var todo = el.closest('li').data('todo');
todo.attr('complete', el.is(':checked'));
}
})
var todoStore = can.fixture.store(5, function(i) {
return {
id: i,
name: 'Todo ' + i,
complete: false
}
})
can.fixture({
'GET /todos': todoStore.findAll,
'GET /todos/{id}': todoStore.findOne,
'POST /todos': todoStore.create,
'PUT /todos/{id}': todoStore.update,
'DELETE /todos/{id}': todoStore.destroy
});
var todos = new Todo.List({});
new TodoList('#todos', {todos: todos});
// Todo.findAll({}, function(todos) {
// new TodoList('#todos', {});
// new TodoList('#todos', {todos: todos });
// });
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment