Last active
April 29, 2019 23:46
-
-
Save kstulgys/c050c849def9a02e44fac9fefa17a87c to your computer and use it in GitHub Desktop.
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
export default class TodosModel { | |
constructor(pubSub) { | |
this.todos = [ | |
{ title: "clean room", id: 1, done: false }, | |
{ title: "read book", id: 2, done: false }, | |
{ title: "learn classes", id: 3, done: false } | |
]; | |
this.pubSub = pubSub; | |
} | |
addItem(item) { | |
const findItem = this.todos.findIndex(el => el.id === item.id) > -1; | |
if (!findItem) { | |
this.todos = [...this.todos, item]; | |
} | |
this.pubSub.emit("addItem", item); | |
} | |
deleteItem(id) { | |
this.todos = this.todos.filter(item => item.id !== id); | |
this.pubSub.emit("deleteItem", id); | |
} | |
markAsDone(id) { | |
this.todos.find(el => el.id === id).done = true; | |
this.pubSub.emit("markAsDone", id); | |
} | |
itemsDone() { | |
const itemsDone = this.todos.filter(item => item.done === true); | |
this.pubSub.emit("itemsDone", itemsDone); | |
} | |
allItems() { | |
this.pubSub.emit("allItems", this.todos); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment