Created
March 28, 2014 22:52
-
-
Save gunnarahlberg/9844657 to your computer and use it in GitHub Desktop.
Todo app with Sort
This file contains 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
<!doctype html> | |
<script src= "mithril.js"></script> | |
<script> | |
//application modules | |
var todo = {}; | |
//todo properties | |
todo.Todo = function(data) { | |
this.description = m.prop(data.description); | |
this.done = m.prop(false); | |
m.redraw(); | |
}; | |
todo.TodoList = Array; | |
todo.controller = function() { | |
this.list = new todo.TodoList(); | |
this.description = m.prop(""); | |
this.add = function(description) { | |
if(description()) { | |
this.list.push(new todo.Todo({description:description()})); | |
this.description(""); | |
}; | |
}; | |
}; | |
todo.view = function(ctrl) { | |
return m("html", [ | |
m("body", [ | |
m("input", {onchange:m.withAttr("value", ctrl.description), value:ctrl.description()}), | |
m("button", {onclick:ctrl.add.bind(ctrl, ctrl.description)}, "Add"), | |
m("table", [ | |
ctrl.list.sort(function(task, index) {return task.done();}). | |
map(function (task, index) { | |
return m("tr", [ | |
m("td", [ | |
m("input[type=checkbox]", {onclick:m.withAttr("checked", task.done), checked:task.done()}) | |
]), | |
m("td", | |
{style: { | |
textDecoration : task.done() ? | |
"line-through": | |
"none" | |
}}, task.description()), | |
]) | |
}) | |
]) | |
]) | |
]); | |
}; | |
m.module(document, todo); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment