Skip to content

Instantly share code, notes, and snippets.

@funador
Created January 20, 2019 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funador/45b6baba42e7ad126b526bd34b0c488a to your computer and use it in GitHub Desktop.
Save funador/45b6baba42e7ad126b526bd34b0c488a to your computer and use it in GitHub Desktop.
const store = (() => {
const todos = []
function addToStore(todo) {
todo.editing = false
this.todos = [todo, ...this.todos]
}
function deleteFromStore(id) {
this.todos = this.todos.filter(todo => todo.id !== id)
}
function setEditing(id) {
this.todos = this.todos.map(todo => {
if(todo.id == id) {
todo.editing = !todo.editing
}
return todo
})
}
function findById(id) {
return this.todos.find(todo => todo.id == id)
}
function updateInStore(updated) {
this.todos = this.todos.map(todo =>
todo.id == updated.id
? Object.assign(todo, updated)
: todo
)
}
function addTodosOnLoad(todos) {
this.todos = todos.reverse().map(todo => {
todo.editing = false
return todo
})
}
return {
todos,
addToStore,
deleteFromStore,
setEditing,
findById,
updateInStore,
addTodosOnLoad
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment