Skip to content

Instantly share code, notes, and snippets.

@jtulach
Forked from rosvit/Todo.java
Last active November 5, 2017 05:50
Show Gist options
  • Save jtulach/9891256 to your computer and use it in GitHub Desktop.
Save jtulach/9891256 to your computer and use it in GitHub Desktop.
ToDo Example with HTML/Java API
<html>
<head>
</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> ]
<ul class="unstyled" data-bind="foreach: todos">
<li>
<input type="checkbox" data-bind="checked: done">
<span class="done-{{todo.done}}" data-bind="text: text"></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>
package dew.demo.todos;
import net.java.html.json.*;
import java.util.List;
import java.util.ArrayList;
@Model(className="TodoUI", properties={
@Property(name="todos", 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) {
ArrayList<Todo> old = new ArrayList<>(m.getTodos());
m.getTodos().clear();
for (Todo d : old) {
if (!d.isDone()) {
m.getTodos().add(d);
}
}
}
@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();
}
}
@jtulach
Copy link
Author

jtulach commented Nov 5, 2017

See this code snippet in action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment