-
-
Save mhoennig/2594056c52a0227e64fb to your computer and use it in GitHub Desktop.
Angular ToDo Example in DukeScript
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
<html> | |
<head> | |
<style> | |
.isDone { | |
text-decoration: line-through; | |
color: grey; | |
} | |
</style> | |
</head> | |
<body> | |
<h2>Todo</h2> | |
<div ng-controller="TodoCtrl"> | |
<span><span data-bind="text: remaining"></span> of | |
<span data-bind="text: todos().length"></span> | |
remaining</span> | |
[ <a href="#" data-bind="click: archive">archive</a> ] | |
[ <a href="#" data-bind="click: restore">restore</a> ] | |
<ul class="unstyled" data-bind="foreach: todos"> | |
<li> | |
<input type="checkbox" data-bind="checked: done"> | |
<span data-bind="text: text, css: { isDone: done }"></span> | |
</li> | |
</ul> | |
<form> | |
<input type="text" data-bind="value: todoText, valueUpdate: 'afterkeydown'" size="30" | |
placeholder="add new todo here"> | |
<input type="submit" value="add" data-bind="click: addTodo, enable: todoText"> | |
</form> | |
</div> | |
</body> | |
</html> |
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
package dew.demo.todos; | |
import net.java.html.json.*; | |
import java.util.List; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
@Model(className="TodoUI", properties={ | |
@Property(name="todos", type=Todo.class, array=true), | |
@Property(name="archivedTodos", type=Todo.class, array=true), | |
@Property(name="todoText", type=String.class) | |
}) | |
class TodoCtrl { | |
@Model(className="Todo", properties={ | |
@Property(name="text", type=String.class), | |
@Property(name="done", type=boolean.class) | |
}) | |
static class ItemCtrl { | |
} | |
@ComputedProperty static int remaining( | |
List<Todo> todos, String todoText) { | |
int count = 0; | |
for (Todo d : todos) { | |
if (!d.isDone()) { | |
count++; | |
} | |
} | |
return count; | |
} | |
@Function static void archive(TodoUI m) { | |
for (Iterator<Todo> iterator = m.getTodos().iterator(); iterator.hasNext(); ) { | |
Todo todo = iterator.next(); | |
if (todo.isDone()) { | |
iterator.remove(); | |
m.getArchivedTodos().add(todo); | |
} | |
} | |
} | |
@Function static void restore(TodoUI m) { | |
int n = 0; | |
for (Iterator<Todo> iterator = m.getArchivedTodos().iterator(); iterator.hasNext(); ) { | |
Todo todo = iterator.next(); | |
m.getTodos().add(n++, todo); | |
} | |
m.getArchivedTodos().clear(); | |
} | |
@Function static void addTodo(TodoUI m) { | |
m.getTodos().add(new Todo(m.getTodoText(), false)); | |
m.setTodoText(null); | |
} | |
// initialize the UI | |
static { | |
new TodoUI( | |
null, | |
new Todo("First", false), | |
new Todo("2nd", true), | |
new Todo("Third", false) | |
).applyBindings(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment