-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial.sjs
78 lines (70 loc) · 2.23 KB
/
tutorial.sjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var data = [
{author: "Pete Hunt", text: "This is one comment"},
{author: "Jordan Walke", text: "This is *another* comment"}
];
var converter = new Showdown.converter();
rclass Comment = {
render: function() {
var rawMarkup = converter.makeHtml(this.props.children.toString());
return hiccup
[div.comment
[h2.commentAuthor (this.props.author)]
[span {dangerouslySetInnerHTML:({__html: rawMarkup})}]];
}
}
rclass CommentList = {
render: function() {
var commentNodes = this.props.data.map(function (comment,i) {
return hiccup [Comment {key: i, author: comment.author} (comment.text)];
});
return hiccup [div.commentList commentNodes];
}
}
rclass CommentForm = {
handleSubmit: function() {
var author = this.refs.author.getDOMNode().value.trim();
var text = this.refs.text.getDOMNode().value.trim();
this.props.onCommentSubmit({author:author, text:text});
this.refs.author.getDOMNode().value = '';
this.refs.text.getDOMNode().value = '';
return false;
},
render: function() {
return hiccup
[form.commentForm {onSubmit: this.handleSubmit}
[input {type: "text", placeholder: "Your name", ref: "author"}]
[input {type: "text", placeholder: "Say something...", ref: "text"}]
[input {type: "submit", value:"Post"}]]
}
}
rclass CommentBox = {
loadCommentsFromServer: function() {
$.ajax({
url: this.props.url,
success: function(data) {
this.setState({data: data});
}.bind(this)
});
},
handleCommentSubmit: function(comment) {
var comments = this.state.data;
var newComments = comments.concat([comment]);
this.setState({data: newComments});
},
getInitialState: function() {
return {data: []};
},
componentWillMount: function() {
this.loadCommentsFromServer();
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
},
render: function() {
return hiccup [div.commentBox
[h1 "Comments"]
[CommentList {data: this.state.data}]
[CommentForm {onCommentSubmit: this.handleCommentSubmit}]]}
}
React.renderComponent(
hiccup [CommentBox {url: "comments.json", pollInterval: 2000}],
document.getElementById('content')
);