import { Injectable } from 'angular2/core' ; export interface Todo { completed : Boolean ; editing : Boolean ; title : String ; } @ Injectable ( ) export class TodoStore { todos : Array < Todo > ; constructor ( ) { this . todos = JSON . parse ( localStorage . getItem ( 'angular2-todos' ) || '[]' ) ; } private updateStore ( ) { localStorage . setItem ( 'angular2-todos' , JSON . stringify ( this . todos ) ) ; } private getWithCompleted ( completed ) { return this . todos . filter ( ( todo ) => todo . completed === completed ) ; } allCompleted ( ) { return this . todos . length === this . getCompleted ( ) . length ; } setAllTo ( completed ) { this . todos . forEach ( ( t ) => t . completed = completed ) ; this . updateStore ( ) ; } removeCompleted ( ) { this . todos = this . getWithCompleted ( false ) ; this . updateStore ( ) ; } getRemaining ( ) { return this . getWithCompleted ( false ) ; } getCompleted ( ) { return this . getWithCompleted ( true ) ; } toggleCompletion ( todo ) { todo . completed = ! todo . completed ; this . updateStore ( ) ; } remove ( todo ) { this . todos . splice ( this . todos . indexOf ( todo ) , 1 ) ; this . updateStore ( ) ; } add ( title ) { const todo = < Todo > { title : title } ; this . todos . push ( todo ) ; this . updateStore ( ) ; } }