|
1 | 1 | import 'css/app.css' |
2 | 2 | import React from 'nestedreact' |
3 | 3 | import ReactDOM from 'react-dom' |
4 | | -import { ToDo, LocalStorage } from './model.js' |
| 4 | +import {ToDo} from './model.js' |
5 | 5 | import TodoList from './todolist.jsx' |
6 | 6 | import Filter from './filter.jsx' |
7 | 7 | import AddTodo from './addtodo.jsx' |
8 | 8 |
|
9 | 9 | const App = React.createClass( { |
10 | | - Model : LocalStorage, |
11 | | - |
| 10 | + // Declare component state |
12 | 11 | state : { |
13 | | - id : 'todo-mvc', |
14 | 12 | todos : ToDo.Collection, |
15 | | - filterDone : Boolean.value( null ) |
| 13 | + filterDone : Boolean.value( null ) // null | true | false, initialized with null. |
16 | 14 | }, |
17 | 15 |
|
18 | 16 | componentWillMount(){ |
19 | | - this.state.fetch(); |
20 | | - window.onunload = () => this.state.save(); |
| 17 | + const { state } = this, |
| 18 | + // load raw JSON from local storage |
| 19 | + json = JSON.parse( localStorage.getItem( 'todo-mvc' ) || "{}" ); |
| 20 | + |
| 21 | + // initialize state with raw JSON |
| 22 | + state.set( json, { parse : true } ); |
| 23 | + |
| 24 | + window.onunload = () =>{ |
| 25 | + // Save state back to the local storage |
| 26 | + localStorage.setItem( 'todo-mvc', JSON.stringify( state ) ); |
| 27 | + } |
21 | 28 | }, |
22 | 29 |
|
23 | 30 | render(){ |
24 | | - const { state } = this, |
25 | | - hasTodos = Boolean( state.todos.length ); |
| 31 | + const { todos, filterDone } = this.state, |
| 32 | + hasTodos = Boolean( todos.length ); |
26 | 33 |
|
27 | 34 | return ( |
28 | 35 | <div> |
29 | 36 | <section className="todoapp"> |
30 | | - <AddTodo onEnter={ desc => state.todos.add({ desc : desc }) }/> |
| 37 | + <AddTodo onEnter={ desc => todos.add({ desc : desc }) }/> |
31 | 38 |
|
32 | | - { hasTodos && <TodoList todos={ state.todos } |
33 | | - filterDone={ state.filterDone }/> } |
| 39 | + { hasTodos && <TodoList todos={ todos } |
| 40 | + filterDone={ filterDone }/> } |
34 | 41 |
|
35 | | - { hasTodos && <Filter count={ state.todos.activeCount } |
36 | | - filterLink={ state.getLink( 'filterDone' )} |
37 | | - onClear={ () => state.todos.clearCompleted() } |
| 42 | + { hasTodos && <Filter count={ todos.activeCount } |
| 43 | + filterLink={ this.state.getLink( 'filterDone' ) } |
| 44 | + onClear={ () => todos.clearCompleted() } |
38 | 45 | />} |
39 | 46 | </section> |
40 | 47 |
|
|
0 commit comments