app.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. var React = require('react');
  3. var converter = new Showdown.converter();
  4. var Comment = React.createClass({
  5. render: function() {
  6. var rawMarkup = converter.makeHtml(this.props.children.toString());
  7. return (
  8. <div className="comment">
  9. <h2 className="commentAuthor">
  10. {this.props.author}
  11. </h2>
  12. <span dangerouslySetInnerHTML={{__html: rawMarkup}} />
  13. </div>
  14. );
  15. }
  16. });
  17. var CommentList = React.createClass({
  18. render: function () {
  19. var commentNodes = this.props.data.map(function(comment) {
  20. return (
  21. <Comment author={comment.author}>
  22. {comment.text}
  23. </Comment>
  24. );
  25. });
  26. return (
  27. <div className="commentList" >
  28. {commentNodes}
  29. </div>
  30. );
  31. }
  32. });
  33. var CommentForm = React.createClass({
  34. render: function() {
  35. return (
  36. <div className="commentForm">
  37. Hello, world! I am a CommentForm.
  38. </div>
  39. );
  40. }
  41. });
  42. var CommentBox = React.createClass({
  43. getInitialState: function() {
  44. return {data: []};
  45. },
  46. componentDidMount: function() {
  47. $.ajax({
  48. url: this.props.url,
  49. dataType: 'json',
  50. success: function(data) {
  51. this.setState({data: data});
  52. }.bind(this),
  53. error: function(xhr, status, err) {
  54. console.error(this.props.url, status, err.toString());
  55. }.bind(this)
  56. });
  57. },
  58. render() {
  59. return (
  60. <div className="commentBox" >
  61. <h1>Comments</h1>
  62. <CommentList data={this.state.data} />
  63. <CommentForm />
  64. </div>
  65. );
  66. }
  67. });
  68. module.exports = CommentBox;