|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta http-equiv='Content-type' content='text/html; charset=utf-8'> |
| 5 | + <title>Hello React</title> |
| 6 | + |
| 7 | + <!-- React JS --> |
| 8 | + <script src="bower_components/react/react.js"></script> |
| 9 | + <script src="bower_components/react/JSXTransformer.js"></script> |
| 10 | + |
| 11 | + <!-- Firebase JS --> |
| 12 | + <script src="bower_components/firebase/firebase.js"></script> |
| 13 | + |
| 14 | + <!-- ReactFireMixin --> |
| 15 | + <script src="bower_components/ReactFire/js/ReactFireMixin.js"></script> |
| 16 | + |
| 17 | + <!-- Markdown --> |
| 18 | + <script src="bower_components/showdown/compressed/showdown.js"></script> |
| 19 | + |
| 20 | + </head> |
| 21 | + <body> |
| 22 | + |
| 23 | + <div id="content"></div> |
| 24 | + |
| 25 | + <script type="text/jsx"> |
| 26 | + /** |
| 27 | + * @jsx React.DOM |
| 28 | + */ |
| 29 | + // The above declaration must remain intact at the top of the script. |
| 30 | + |
| 31 | + // IMPORTANT: Replace below with your Firebase app URL |
| 32 | + var firebaseApp = "https://my-firebase-app.firebaseio.com/"; |
| 33 | + |
| 34 | + var converter = new Showdown.converter(); |
| 35 | + |
| 36 | + var Comment = React.createClass({ |
| 37 | + render: function() { |
| 38 | + var rawMarkup = converter.makeHtml(this.props.children.toString()); |
| 39 | + return ( |
| 40 | + <div className="comment"> |
| 41 | + <h2 className="commentAuthor">{this.props.author}</h2> |
| 42 | + <span dangerouslySetInnerHTML={{__html: rawMarkup}} /> |
| 43 | + </div> |
| 44 | + ); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + var CommentList = React.createClass({ |
| 49 | + render: function() { |
| 50 | + var commentNodes = this.props.data.map(function (comment, index) { |
| 51 | + return <Comment key={index} author={comment.author}>{comment.text}</Comment>; |
| 52 | + }); |
| 53 | + return <div className="commentList">{commentNodes}</div>; |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + var CommentForm = React.createClass({ |
| 58 | + handleSubmit: function() { |
| 59 | + var author = this.refs.author.getDOMNode().value.trim(); |
| 60 | + var text = this.refs.text.getDOMNode().value.trim(); |
| 61 | + this.props.onCommentSubmit({author: author, text: text}); |
| 62 | + this.refs.author.getDOMNode().value = ''; |
| 63 | + this.refs.text.getDOMNode().value = ''; |
| 64 | + return false; |
| 65 | + }, |
| 66 | + render: function() { |
| 67 | + return ( |
| 68 | + <form className="commentForm" onSubmit={this.handleSubmit}> |
| 69 | + <input type="text" placeholder="Your name" ref="author" /> |
| 70 | + <input type="text" placeholder="Say something..." ref="text" /> |
| 71 | + <input type="submit" value="Post" /> |
| 72 | + </form> |
| 73 | + ); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + var CommentBox = React.createClass({ |
| 78 | + mixins: [ReactFireMixin], |
| 79 | + |
| 80 | + handleCommentSubmit: function(comment) { |
| 81 | + var comments = this.state.data; |
| 82 | + comments.push(comment); |
| 83 | + this.setState({data: comments}); |
| 84 | + |
| 85 | + // Here we push the update out to Firebase |
| 86 | + this.firebaseRefs["data"].push(comment); |
| 87 | + }, |
| 88 | + getInitialState: function() { |
| 89 | + return {data: []}; |
| 90 | + }, |
| 91 | + componentWillMount: function() { |
| 92 | + // Here we bind the component to Firebase and it handles all data updates, |
| 93 | + // no need to poll as in the React example. |
| 94 | + this.bindAsArray(new Firebase(firebaseApp + "commentBox"), "data"); |
| 95 | + }, |
| 96 | + render: function() { |
| 97 | + return ( |
| 98 | + <div className="commentBox"> |
| 99 | + <h1>Comments</h1> |
| 100 | + <CommentList data={this.state.data} /> |
| 101 | + <CommentForm onCommentSubmit={this.handleCommentSubmit} /> |
| 102 | + </div> |
| 103 | + ); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + React.renderComponent( |
| 108 | + <CommentBox />, |
| 109 | + document.getElementById('content') |
| 110 | + ); |
| 111 | + |
| 112 | + |
| 113 | + </script> |
| 114 | + |
| 115 | + </body> |
| 116 | +</html> |
0 commit comments