1
+ var data = [
2
+ { id : 1 , author : "Pete Hunt" , text : "This is one comment" } ,
3
+ { id : 2 , author : "Jordan Walke" , text : "This is *another* comment" }
4
+ ]
5
+
6
+ // react tutorial
7
+ var CommentBox = React . createClass ( {
8
+ loadCommentsFromSever : function ( ) {
9
+ $ . ajax ( {
10
+ url : this . props . url ,
11
+ dataType : 'json' ,
12
+ cache : false ,
13
+ success : function ( data ) {
14
+ this . setState ( { data : data } )
15
+ } . bind ( this ) ,
16
+ error : function ( xhr , status , err ) {
17
+ console . error ( this . props . url , status , err . toString ( ) )
18
+ } . bind ( this )
19
+ } )
20
+ } ,
21
+
22
+ handleCommentSubmit : function ( comment ) {
23
+ var comments = this . state . data
24
+
25
+ // optimistic
26
+ comment . id = Date . now ( )
27
+ var newComments = comments . concat ( [ comment ] )
28
+ this . setState ( { data : newComments } )
29
+
30
+ $ . ajax ( {
31
+ url : this . props . url ,
32
+ dataType : 'json' ,
33
+ type : 'POST' ,
34
+ data : comment ,
35
+ success : function ( data ) {
36
+ this . setState ( { data : data } )
37
+ } . bind ( this ) ,
38
+ error : function ( xhr , status , err ) {
39
+ this . setState ( { data : comments } )
40
+ console . error ( this . props . url , status , err . toString ( ) )
41
+ } . bind ( this )
42
+ } )
43
+ } ,
44
+
45
+ getInitialState : function ( ) {
46
+ return { data :[ ] }
47
+ } ,
48
+
49
+ componentDidMount : function ( ) {
50
+ this . loadCommentsFromSever ( )
51
+ setInterval ( this . loadCommentsFromSever , this . props . pollInterval ) ;
52
+ } ,
53
+
54
+ render : function ( ) {
55
+ return (
56
+ < div className = "commentBox" >
57
+ < h1 > Comments</ h1 >
58
+ < CommentList data = { this . state . data } />
59
+ < CommentForm onCommentSubmit = { this . handleCommentSubmit } />
60
+ </ div >
61
+ )
62
+ }
63
+ } )
64
+
65
+ var CommentList = React . createClass ( {
66
+ render : function ( ) {
67
+ var commentNodes = this . props . data . map ( function ( comment ) {
68
+ return (
69
+ < Comment author = { comment . author } key = { comment . id } >
70
+ { comment . text }
71
+ </ Comment >
72
+ )
73
+ } )
74
+ return (
75
+ < div className = "commentList" >
76
+ { commentNodes }
77
+ </ div >
78
+ )
79
+ }
80
+ } )
81
+
82
+ var CommentForm = React . createClass ( {
83
+ getInitialState : function ( ) {
84
+ return { author : '' , text : '' }
85
+ } ,
86
+
87
+ handleAuthorChange : function ( e ) {
88
+ this . setState ( { author : e . target . value } )
89
+ } ,
90
+
91
+ handleTextChange : function ( e ) {
92
+ this . setState ( { text : e . target . value } )
93
+ } ,
94
+
95
+ handleSubmit : function ( e ) {
96
+ e . preventDefault ( )
97
+ var author = this . state . author . trim ( )
98
+ var text = this . state . text . trim ( )
99
+ if ( ! text || ! author ) {
100
+ return
101
+ }
102
+ this . props . onCommentSubmit ( { author : author , text : text } )
103
+ this . setState ( { author : '' , text : '' } )
104
+ } ,
105
+
106
+ render : function ( ) {
107
+ return (
108
+ < form className = "commentForm" onSubmit = { this . handleSubmit } >
109
+ < input
110
+ type = "text"
111
+ placeholder = "Your name"
112
+ value = { this . state . author }
113
+ onChange = { this . handleAuthorChange }
114
+ />
115
+ < input
116
+ type = "text"
117
+ placeholder = "Say something..."
118
+ value = { this . state . text }
119
+ onChange = { this . handleTextChange }
120
+ />
121
+ < input type = "submit" value = "Post" />
122
+ </ form >
123
+ )
124
+ }
125
+ } )
126
+
127
+ var Comment = React . createClass ( {
128
+ rawMarkup : function ( ) {
129
+ var rawMarkup = marked ( this . props . children . toString ( ) , { sanitize : true } )
130
+ return { __html : rawMarkup }
131
+ } ,
132
+
133
+ render : function ( ) {
134
+ return (
135
+ < div className = "comment" >
136
+ < h2 className = "commentAuthor" >
137
+ { this . props . author }
138
+ </ h2 >
139
+ < span dangerouslySetInnerHTML = { this . rawMarkup ( ) } />
140
+ </ div >
141
+ )
142
+ }
143
+ } )
144
+
145
+ ReactDOM . render (
146
+ < CommentBox url = "/api/comments" pollInterval = { 2000 } /> ,
147
+ document . getElementById ( 'content' )
148
+ )
0 commit comments