import { observable } from 'mobx' ; import { IViewModel , EventEmitter , input , output } from '../../src/index' ; import { Todo } from './Todo' ; export class TodoViewModel implements IViewModel { @ observable todos : Todo [ ] = [ ] ; @ input @ observable user : string = '' ; @ output onTodoAdded = new EventEmitter < Todo > ( ) ; @ output onTodoSaved = new EventEmitter < Todo [ ] > ( ) ; constructor ( ) { if ( window . localStorage ) { const json = JSON . parse ( window . localStorage . getItem ( "todos" ) || "[]" ) ; this . todos = json . map ( todo => Todo . deserialize ( todo ) ) ; } } save ( ) { if ( window . localStorage ) { window . localStorage . setItem ( "todos" , JSON . stringify ( this . todos . map ( todo => todo . serialize ( ) ) ) ) ; } this . onTodoSaved . emit ( this . todos ) ; } add ( ) { const newTodo = new Todo ( ) ; this . todos . push ( newTodo ) ; this . onTodoAdded . emit ( newTodo ) ; } remove ( todo ) { const index = this . todos . indexOf ( todo ) ; if ( index > - 1 ) { this . todos . splice ( index , 1 ) ; } } }