Skip to content

Instantly share code, notes, and snippets.

@ksafranski
Created February 25, 2018 20:12
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 ksafranski/ee14d1e4dfac882670672294f92d5d59 to your computer and use it in GitHub Desktop.
Save ksafranski/ee14d1e4dfac882670672294f92d5d59 to your computer and use it in GitHub Desktop.
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))
}
}
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