|
| 1 | +/** @jsx React.DOM */ |
| 2 | + |
| 3 | +var converter = new Showdown.converter(); |
| 4 | + |
| 5 | +var Comment = React.createClass({ |
| 6 | + render: function() { |
| 7 | + var rawMarkup = converter.makeHtml(this.props.children.toString()); |
| 8 | + return ( |
| 9 | + <div className="comment"> |
| 10 | + <h2 className="commentAuthor"> |
| 11 | + {this.props.author} |
| 12 | + </h2> |
| 13 | + <span dangerouslySetInnerHTML={{__html: rawMarkup}} /> |
| 14 | + </div> |
| 15 | + ); |
| 16 | + } |
| 17 | +}); |
| 18 | + |
| 19 | +var CommentBox = React.createClass({ |
| 20 | + loadCommentsFromServer: function() { |
| 21 | + $.ajax({ |
| 22 | + url: this.props.url, |
| 23 | + dataType: 'json', |
| 24 | + success: function(data) { |
| 25 | + this.setState({data: data}); |
| 26 | + }.bind(this), |
| 27 | + error: function(xhr, status, err) { |
| 28 | + console.error(this.props.url, status, err.toString()); |
| 29 | + }.bind(this) |
| 30 | + }); |
| 31 | + }, |
| 32 | + handleCommentSubmit: function(comment) { |
| 33 | + var comments = this.state.data; |
| 34 | + comments.push(comment); |
| 35 | + this.setState({data: comments}, function() { |
| 36 | + // `setState` accepts a callback. To avoid (improbable) race condition, |
| 37 | + // `we'll send the ajax request right after we optimistically set the new |
| 38 | + // `state. |
| 39 | + $.ajax({ |
| 40 | + url: this.props.url, |
| 41 | + dataType: 'json', |
| 42 | + type: 'POST', |
| 43 | + data: comment, |
| 44 | + success: function(data) { |
| 45 | + this.setState({data: data}); |
| 46 | + }.bind(this), |
| 47 | + error: function(xhr, status, err) { |
| 48 | + console.error(this.props.url, status, err.toString()); |
| 49 | + }.bind(this) |
| 50 | + }); |
| 51 | + }); |
| 52 | + }, |
| 53 | + getInitialState: function() { |
| 54 | + return {data: []}; |
| 55 | + }, |
| 56 | + componentWillMount: function() { |
| 57 | + this.loadCommentsFromServer(); |
| 58 | + setInterval(this.loadCommentsFromServer, this.props.pollInterval); |
| 59 | + }, |
| 60 | + render: function() { |
| 61 | + return ( |
| 62 | + <div className="commentBox"> |
| 63 | + <h1>Comments</h1> |
| 64 | + <CommentList data={this.state.data} /> |
| 65 | + <CommentForm onCommentSubmit={this.handleCommentSubmit} /> |
| 66 | + </div> |
| 67 | + ); |
| 68 | + } |
| 69 | +}); |
| 70 | + |
| 71 | +var CommentList = React.createClass({ |
| 72 | + render: function() { |
| 73 | + var commentNodes = this.props.data.map(function(comment, index) { |
| 74 | + return ( |
| 75 | + // `key` is a React-specific concept and is not mandatory for the |
| 76 | + // purpose of this tutorial. if you're curious, see more here: |
| 77 | + // http://facebook.github.io/react/docs/multiple-components.html#dynamic-children |
| 78 | + <Comment author={comment.author} key={index}> |
| 79 | + {comment.text} |
| 80 | + </Comment> |
| 81 | + ); |
| 82 | + }); |
| 83 | + return ( |
| 84 | + <div className="commentList"> |
| 85 | + {commentNodes} |
| 86 | + </div> |
| 87 | + ); |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +var CommentForm = React.createClass({ |
| 92 | + handleSubmit: function() { |
| 93 | + var author = this.refs.author.getDOMNode().value.trim(); |
| 94 | + var text = this.refs.text.getDOMNode().value.trim(); |
| 95 | + this.props.onCommentSubmit({author: author, text: text}); |
| 96 | + this.refs.author.getDOMNode().value = ''; |
| 97 | + this.refs.text.getDOMNode().value = ''; |
| 98 | + return false; |
| 99 | + }, |
| 100 | + render: function() { |
| 101 | + return ( |
| 102 | + <form className="commentForm" onSubmit={this.handleSubmit}> |
| 103 | + <input type="text" placeholder="Your name" ref="author" /> |
| 104 | + <input type="text" placeholder="Say something..." ref="text" /> |
| 105 | + <input type="submit" value="Post" /> |
| 106 | + </form> |
| 107 | + ); |
| 108 | + } |
| 109 | +}); |
| 110 | + |
| 111 | +React.renderComponent( |
| 112 | + <CommentBox url="comments.json" pollInterval={2000} />, |
| 113 | + document.getElementById('content') |
| 114 | +); |
0 commit comments