1+ import React from 'nestedreact'
2+ import { Model } from 'nestedtypes'
3+
4+ // pre-define model, cause we gonna have recursive definition...
15const TodoItem = Model . extend ( ) ;
26
37TodoItem . define ( {
48 defaults : {
59 done : Boolean ,
610 desc : String ,
11+
12+ // this is the tree, with components of the same type...
713 subitems : TodoItem . Collection
814 } ,
915
@@ -16,9 +22,11 @@ TodoItem.define({
1622 } ,
1723
1824 initialize ( ) {
25+ // Pin these methods, cause we gonna use them as click handlers...
1926 _ . bindAll ( this , 'remove' , 'addChildren' ) ;
2027
21- // sync done state with subitems...
28+ // Sync done state with subitems across the tree...
29+ // There won't be loop, 'change' is never fired if there are no actual change.
2230 this . listenTo ( this , {
2331 'change:subitems' : ( ) => {
2432 let { subitems } = this ;
@@ -35,19 +43,22 @@ TodoItem.define({
3543 }
3644} ) ;
3745
38-
3946const App = React . createClass ( {
4047 // define the state...
4148 attributes : {
49+ // deep changes in this tree structure will be detected,
50+ // and this component will be updated automatically.
4251 todos : TodoItem . Collection
4352 } ,
4453
4554 render ( ) {
46- // link state to the application...
55+ // State is already initialized with default values.
56+ // Now, link the state to components...
4757 return < Checklist todos = { this . state . todos } /> ;
4858 }
49- } )
59+ } ) ;
5060
61+ // Everything else gonna be stateless...
5162const Checklist = ( { todos } ) => (
5263 < div className = "checklist" >
5364 { todos . map ( todo => < ToDo key = { todo . cid } todo = { todo } /> ) }
@@ -57,11 +68,14 @@ const Checklist = ({ todos } ) => (
5768const ToDo = ( { todo } ) => (
5869 < div className = "todo-item" >
5970 < 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 ) } / >
71+ < input type = "checkbox" checkedLink = { todo . getLink ( 'done' ) } />
72+ < input type = "text" valueLink = { todo . getLink ( 'desc' ) } />
73+ < div class = 'add-children' onClick = { todo . addChildren } > + </ div >
74+ < div class = 'delete' onClick = { todo . remove } > x </ div >
6475 </ div >
6576 { todo . subitems . length && < Checklist todos = { todo . subitems } /> }
6677 </ div >
6778) ;
79+
80+ // Done.
81+ export default App ;
0 commit comments