Created
September 20, 2013 19:48
-
-
Save imjoshdean/6642864 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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