-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
143 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/*global window*/ | ||
(function(ref) { | ||
ref.actions = function(sendUpdate) { | ||
return { | ||
saveTodo: function(todo) { | ||
sendUpdate({ saveTodo: { title: todo } }); | ||
}, | ||
deleteTodoId: function(todoId) { | ||
sendUpdate({ deleteTodoId: todoId }); | ||
}, | ||
setCompleted: function(todoId, completed) { | ||
sendUpdate({ setCompleted: { id: todoId, completed: completed } }); | ||
} | ||
}; | ||
}; | ||
})(window); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/*global window*/ | ||
(function(ref) { | ||
ref.initialModel = { todoItems: ref.todoStorage.load() }; | ||
ref.initialModel = { todos: ref.todoStorage.loadAll() }; | ||
})(window); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/*global window*/ | ||
(function(ref) { | ||
ref.receiveUpdate = function(model, update) { | ||
if (update.saveTodo) { | ||
model.todos = window.todoStorage.saveTodo(update.saveTodo); | ||
} | ||
else if (update.deleteTodoId) { | ||
model.todos = window.todoStorage.deleteTodoId(update.deleteTodoId); | ||
} | ||
else if (update.setCompleted) { | ||
model.todos = window.todoStorage.setCompleted(update.setCompleted); | ||
} | ||
return model; | ||
}; | ||
})(window); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,67 @@ | ||
(function(ref) { | ||
var STORAGE_KEY = "meiosis-todomvc"; | ||
var nextId = 1; | ||
|
||
var findIndex = function(todos, todoId) { | ||
var index = -1; | ||
|
||
for (var i = 0, t = todos.length; i < t; i++) { | ||
if (todos[i].id === todoId) { | ||
index = i; | ||
break; | ||
} | ||
} | ||
return index; | ||
}; | ||
|
||
var replaceTodoAtIndex = function(todos, todo, index) { | ||
return todos.slice(0, index).concat([todo]).concat(todos.slice(index + 1)); | ||
}; | ||
|
||
var deleteTodoAtIndex = function(todos, index) { | ||
return todos.slice(0, index).concat(todos.slice(index + 1)); | ||
}; | ||
|
||
ref.todoStorage = { | ||
load: function () { | ||
loadAll: function() { | ||
return JSON.parse(localStorage.getItem(STORAGE_KEY) || | ||
"[{\"id\":1,\"title\":\"meiosis\",\"completed\":false}]"); | ||
}, | ||
save: function (todos) { | ||
saveTodo: function(todo) { | ||
var todos = ref.todoStorage.loadAll(); | ||
|
||
if (parseInt(todo.id, 10) > 0) { | ||
todos = replaceTodoAtIndex(todos, todo, findIndex(todos, todo.id)); | ||
} | ||
else { | ||
todo.id = nextId++; | ||
todos = todos.concat([todo]); | ||
} | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos)); | ||
|
||
return todos; | ||
}, | ||
deleteTodoId: function(todoId) { | ||
var todos = ref.todoStorage.loadAll(); | ||
var index = findIndex(todos, todoId); | ||
|
||
if (index >= 0) { | ||
todos = deleteTodoAtIndex(todos, index); | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos)); | ||
} | ||
return todos; | ||
}, | ||
setCompleted: function(updatedTodo) { | ||
var todos = ref.todoStorage.loadAll(); | ||
var index = findIndex(todos, updatedTodo.id); | ||
|
||
if (index >= 0) { | ||
var todo = todos[index]; | ||
todo.completed = updatedTodo.completed; | ||
todos = replaceTodoAtIndex(todos, todo, index); | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos)); | ||
} | ||
return todos; | ||
} | ||
}; | ||
})(window); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
/*global meiosis, meiosisVanillaJs, window*/ | ||
(function() { | ||
(function(ref) { | ||
var Meiosis = meiosis(meiosisVanillaJs.intoSelector("body")); | ||
|
||
var createComponent = Meiosis.createComponent; | ||
|
||
var Main = createComponent({ | ||
initialModel: window.initialModel, | ||
view: window.view | ||
initialModel: ref.initialModel, | ||
view: ref.view, | ||
actions: ref.actions, | ||
ready: ref.ready, | ||
receiveUpdate: ref.receiveUpdate | ||
}); | ||
|
||
Meiosis.run(Main); | ||
})(); | ||
})(window); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/*global meiosisVanillaJs, window*/ | ||
(function(ref) { | ||
var ENTER_KEY = 13; | ||
var ESCAPE_KEY = 27; | ||
|
||
ref.ready = function(actions) { | ||
meiosisVanillaJs.delegate(document.body, "input.new-todo", "keypress", function(evt) { | ||
if (evt.keyCode === ENTER_KEY) { | ||
actions.saveTodo(evt.target.value); | ||
} | ||
else if (evt.keyCode === ESCAPE_KEY) { | ||
// TODO | ||
} | ||
}); | ||
|
||
meiosisVanillaJs.delegate(document.body, "input.toggle", "change", function(evt) { | ||
var todoId = parseInt(evt.target.dataset.id, 10); | ||
var completed = evt.target.checked; | ||
actions.setCompleted(todoId, completed); | ||
}); | ||
|
||
meiosisVanillaJs.delegate(document.body, "button.destroy", "click", function(evt) { | ||
var todoId = parseInt(evt.target.dataset.id, 10); | ||
actions.deleteTodoId(todoId); | ||
}); | ||
}; | ||
})(window); |
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