|
12 | 12 | </head>
|
13 | 13 | <body>
|
14 | 14 | <div id="content"></div>
|
15 |
| - <script type="text/babel" src="scripts/example.js"></script> |
16 | 15 | <script type="text/babel">
|
17 |
| - // To get started with this tutorial running your own code, simply remove |
18 |
| - // the script tag loading scripts/example.js and start writing code here. |
| 16 | + // tutorial1.js |
| 17 | + var CommentBox = React.createClass({ |
| 18 | + getInitialState: function () { |
| 19 | + return {data: []}; |
| 20 | + }, |
| 21 | + loadCommentsFromServer: function () { |
| 22 | + $.ajax({ |
| 23 | + url: this.props.url, |
| 24 | + dataType: 'json', |
| 25 | + cache: false, |
| 26 | + success: function(data) { |
| 27 | + this.setState({data: data}); |
| 28 | + }.bind(this), |
| 29 | + error: function (xhr, status, err) { |
| 30 | + console.error(this.props.url, status, err.toString()); |
| 31 | + }.bind(this) |
| 32 | + }); |
| 33 | + }, |
| 34 | + getInitialState: function () { |
| 35 | + return {data: []}; |
| 36 | + }, |
| 37 | + componentDidMount: function () { |
| 38 | + this.loadCommentsFromServer(); |
| 39 | + setInterval(this.loadCommentsFromServer, this.props.pollInterval); |
| 40 | + } |
| 41 | + render: function () { |
| 42 | + return ( |
| 43 | + <div className="commentBox"> |
| 44 | + <h1>Comments</h1> |
| 45 | + <CommentList data={this.state.data}/> |
| 46 | + <CommentForm /> |
| 47 | + </div> |
| 48 | + ); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + var CommentList = React.createClass({ |
| 53 | + render: function () { |
| 54 | + var commentNodes = this.props.data.map(function (comment) { |
| 55 | + return ( |
| 56 | + <Comment author={comment.author}> |
| 57 | + {comment.text} |
| 58 | + </Comment> |
| 59 | + ); |
| 60 | + }); |
| 61 | + return ( |
| 62 | + <div className="commentList"> |
| 63 | + {commentNodes} |
| 64 | + </div> |
| 65 | + ); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + var CommentForm = React.createClass({ |
| 70 | + render: function () { |
| 71 | + return ( |
| 72 | + <div className="commentForm"> |
| 73 | + Hello, world! I am a CommentForm |
| 74 | + </div> |
| 75 | + ); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + var Comment = React.createClass({ |
| 80 | + rawMarkup: function () { |
| 81 | + var rawMarkup = marked(this.props.children.toString(), {sanitize: true}); |
| 82 | + return { __html: rawMarkup }; |
| 83 | + }, |
| 84 | + |
| 85 | + render: function () { |
| 86 | + return ( |
| 87 | + <div className="comment"> |
| 88 | + <h2 className="commentAuthor"> |
| 89 | + {this.props.author} |
| 90 | + </h2> |
| 91 | + <span dangerouslySetInnerHTML={this.rawMarkup()} /> |
| 92 | + </div> |
| 93 | + ); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + React.render( |
| 100 | + <CommentBox url="/api/comments" pollInterval={2000} /> |
| 101 | + document.getElementById('content') |
| 102 | + ); |
19 | 103 | </script>
|
20 | 104 | </body>
|
21 | 105 | </html>
|
0 commit comments