Created
February 25, 2018 20:12
-
-
Save ksafranski/ee14d1e4dfac882670672294f92d5d59 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
import { observable, computed, action } from 'mobx' | |
import TodoModel from './TodoModel' | |
export default class TodoListModel { | |
@observable todos = [] | |
constructor () { | |
try { | |
this.todos = JSON.parse(window.localStorage.getItem('todos')) | |
} catch (e) { | |
/* swallow */ | |
} | |
} | |
@computed | |
get finishedTodoCount () { | |
return this.todos.filter(todo => todo.finished).length | |
} | |
@computed | |
get percentComplete () { | |
const finished = this.todos.filter(todo => todo.finished).length | |
return Math.floor(finished/this.todos.length * 100) || 0 | |
} | |
@action | |
addTodo (title) { | |
this.todos.push(new TodoModel(title)) | |
window.localStorage.setItem('todos', JSON.stringify(this.todos)) | |
} | |
} |
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
import { observable } from 'mobx' | |
export default class TodoModel { | |
@observable title | |
@observable finished = false | |
constructor (title) { | |
this.title = title | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment