Skip to content

Commit 5e46137

Browse files
author
Matt Calthrop
committed
Add tutorial14
1 parent 6207b23 commit 5e46137

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

public/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<!--<script type="text/babel" src="scripts/tutorial4-5.js"></script>-->
2020
<!--<script type="text/babel" src="scripts/tutorial6-7.js"></script>-->
2121
<!--<script type="text/babel" src="scripts/tutorial8-10.js"></script>-->
22-
<script type="text/babel" src="scripts/tutorial11-13.js"></script>
22+
<!--<script type="text/babel" src="scripts/tutorial11-13.js"></script>-->
23+
<script type="text/babel" src="scripts/tutorial14.js"></script>
2324
</body>
2425
</html>

public/scripts/tutorial14.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* global $, React, ReactDOM */
2+
3+
var Comment = React.createClass({
4+
rawMarkup: function () {
5+
var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
6+
7+
return {__html: rawMarkup};
8+
},
9+
render: function () {
10+
return (
11+
<div className="comment">
12+
<h2 className="commentAuthor">
13+
{this.props.author}
14+
</h2>
15+
<span dangerouslySetInnerHTML={this.rawMarkup()}/>
16+
</div>
17+
);
18+
}
19+
});
20+
21+
var CommentList = React.createClass({
22+
render: function () {
23+
var commentNodes = this.props.data.map(function (comment) {
24+
return (
25+
<Comment key={comment.id} author={comment.author}>
26+
{comment.text}
27+
</Comment>
28+
);
29+
});
30+
31+
return (
32+
<div className="commentList">
33+
{commentNodes}
34+
</div>
35+
);
36+
}
37+
});
38+
39+
var CommentForm = React.createClass({
40+
render: function () {
41+
return (
42+
<div className="commentForm">
43+
Hello, world! I am a CommentForm.
44+
</div>
45+
);
46+
}
47+
});
48+
49+
var CommentBox = React.createClass({
50+
loadCommentsFromServer: function () {
51+
$.ajax({
52+
url: this.props.url,
53+
dataType: 'json',
54+
cache: false,
55+
success: function (data) {
56+
this.setState({data: data});
57+
}.bind(this),
58+
error: function (xhr, status, err) {
59+
console.error(this.props.url, status, err.toString());
60+
}.bind(this)
61+
});
62+
},
63+
getInitialState: function () {
64+
return {
65+
data: []
66+
};
67+
},
68+
componentDidMount: function() {
69+
this.loadCommentsFromServer();
70+
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
71+
},
72+
render: function () {
73+
return (
74+
<div className="commentBox">
75+
<h1>Comments</h1>
76+
<CommentList data={this.state.data}/>
77+
<CommentForm />
78+
</div>
79+
);
80+
}
81+
});
82+
83+
ReactDOM.render(
84+
<CommentBox url="/api/comments" pollInterval={5000}/>,
85+
document.getElementById('content')
86+
);

0 commit comments

Comments
 (0)