|
| 1 | +const TodoItem = Model.extend(); |
| 2 | + |
| 3 | +TodoItem.define({ |
| 4 | + defaults : { |
| 5 | + done : Boolean, |
| 6 | + desc : String, |
| 7 | + subitems : TodoItem.Collection |
| 8 | + }, |
| 9 | + |
| 10 | + remove(){ |
| 11 | + this.collection.remove( this ); |
| 12 | + }, |
| 13 | + |
| 14 | + addChildren(){ |
| 15 | + this.subitems.push( new TodoItem() ); |
| 16 | + }, |
| 17 | + |
| 18 | + initialize(){ |
| 19 | + _.bindAll( this, 'remove', 'addChildren' ); |
| 20 | + |
| 21 | + // sync done state with subitems... |
| 22 | + this.listenTo( this, { |
| 23 | + 'change:subitems' : () => { |
| 24 | + let { subitems } = this; |
| 25 | + |
| 26 | + if( subitems && subitems.length ){ |
| 27 | + this.done = subitems.every( item => item.done ); |
| 28 | + } |
| 29 | + }, |
| 30 | + |
| 31 | + 'change:done' : () => { |
| 32 | + this.subitems.each( item => item.done = this.done ); |
| 33 | + } |
| 34 | + }); |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | + |
| 39 | +const App = React.createClass({ |
| 40 | + // define the state... |
| 41 | + attributes : { |
| 42 | + todos : TodoItem.Collection |
| 43 | + }, |
| 44 | + |
| 45 | + render(){ |
| 46 | + // link state to the application... |
| 47 | + return <Checklist todos={ this.state.todos } />; |
| 48 | + } |
| 49 | +}) |
| 50 | + |
| 51 | +const Checklist = ({ todos } ) => ( |
| 52 | + <div className="checklist"> |
| 53 | + { todos.map( todo => <ToDo key={ todo.cid } todo={ todo } /> )} |
| 54 | + </div> |
| 55 | +); |
| 56 | + |
| 57 | +const ToDo = ({ todo }) => ( |
| 58 | + <div className="todo-item"> |
| 59 | + <div className='header'> |
| 60 | + <input type="checkbox" checkedLink={ todo.getLink( 'done' ) } /> |
| 61 | + <input type="text" valueLink={ todo.getLink( 'desc' ) } /> |
| 62 | + <div class='add-children' onClick={ todo.addChildren ) } /> |
| 63 | + <div class='delete' onClick={ todo.remove ) } /> |
| 64 | + </div> |
| 65 | + { todo.subitems.length && <Checklist todos={ todo.subitems } /> } |
| 66 | + </div> |
| 67 | +); |
0 commit comments