todomvc: localStorage
This commit is contained in:
parent
2c351d7352
commit
3d13375b20
|
@ -14,6 +14,7 @@
|
||||||
assertion type todoExists(id);
|
assertion type todoExists(id);
|
||||||
assertion type todo(id, title, completed);
|
assertion type todo(id, title, completed);
|
||||||
|
|
||||||
|
message type createTodo(title);
|
||||||
message type setTitle(id, title);
|
message type setTitle(id, title);
|
||||||
message type setCompleted(id, completed);
|
message type setCompleted(id, completed);
|
||||||
message type deleteTodo(id);
|
message type deleteTodo(id);
|
||||||
|
@ -31,15 +32,11 @@ assertion type show(completed);
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
var nextId = 0;
|
function todoListItemModel(initialId, initialTitle, initialCompleted) {
|
||||||
function todoListItemModel(title) {
|
|
||||||
title = title.trim();
|
|
||||||
if (!title) return;
|
|
||||||
|
|
||||||
actor {
|
actor {
|
||||||
this.id = nextId++;
|
this.id = initialId;
|
||||||
this.title = title;
|
this.title = initialTitle;
|
||||||
this.completed = false;
|
this.completed = initialCompleted;
|
||||||
|
|
||||||
react {
|
react {
|
||||||
assert todoExists(this.id);
|
assert todoExists(this.id);
|
||||||
|
@ -128,7 +125,8 @@ ground dataspace G {
|
||||||
actor {
|
actor {
|
||||||
react {
|
react {
|
||||||
on message Syndicate.UI.globalEvent('.new-todo', 'change', $e) {
|
on message Syndicate.UI.globalEvent('.new-todo', 'change', $e) {
|
||||||
todoListItemModel(e.target.value);
|
var newTitle = e.target.value.trim();
|
||||||
|
if (newTitle) :: createTodo(newTitle);
|
||||||
e.target.value = "";
|
e.target.value = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -202,9 +200,43 @@ ground dataspace G {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
todoListItemModel('Buy milk');
|
actor {
|
||||||
todoListItemModel('Buy bread');
|
var db;
|
||||||
todoListItemModel('Finish PhD');
|
|
||||||
|
if ('todos-syndicate' in localStorage) {
|
||||||
|
db = JSON.parse(localStorage['todos-syndicate']);
|
||||||
|
for (var i in db.todos) {
|
||||||
|
var t = db.todos[i];
|
||||||
|
todoListItemModel(t.id, t.title, t.completed);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
db = {nextId: 0, todos: {}};
|
||||||
|
react until {
|
||||||
|
case asserted Syndicate.observe(createTodo(_)) {
|
||||||
|
:: createTodo('Buy milk');
|
||||||
|
:: createTodo('Buy bread');
|
||||||
|
:: createTodo('Finish PhD');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
react {
|
||||||
|
on message createTodo($title) {
|
||||||
|
todoListItemModel(db.nextId++, title, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
during todo($id, $title, $completed) {
|
||||||
|
do {
|
||||||
|
db.todos[id] = {id: id, title: title, completed: completed};
|
||||||
|
localStorage['todos-syndicate'] = JSON.stringify(db);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
delete db.todos[id];
|
||||||
|
localStorage['todos-syndicate'] = JSON.stringify(db);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// G.dataspace.setOnStateChange(function (mux, patch) {
|
// G.dataspace.setOnStateChange(function (mux, patch) {
|
||||||
|
|
Loading…
Reference in New Issue