From 51f7375da343bcd3a94f5e4263e9e9c1f57a9eb0 Mon Sep 17 00:00:00 2001 From: Devin Brown Date: Sat, 22 Aug 2015 21:16:25 -0600 Subject: [PATCH 1/3] changed things... --- public/index.html | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/public/index.html b/public/index.html index c6220169..a9823576 100644 --- a/public/index.html +++ b/public/index.html @@ -11,6 +11,43 @@
- + From b388601ee4136a349a61ce33a76a97e10c9bb6aa Mon Sep 17 00:00:00 2001 From: Devin Brown Date: Sun, 23 Aug 2015 16:58:33 -0600 Subject: [PATCH 2/3] fixed errors in the previous commit --- public/comments.json | 4 ++++ public/index.html | 54 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 public/comments.json diff --git a/public/comments.json b/public/comments.json new file mode 100644 index 00000000..119f92d8 --- /dev/null +++ b/public/comments.json @@ -0,0 +1,4 @@ +[ + {"author": "Pete Hunt", "text": "This is one comment"}, + {"author": "Jordan Walke", "text": "This is *another* comment!"} +] \ No newline at end of file diff --git a/public/index.html b/public/index.html index a9823576..49aa49ed 100644 --- a/public/index.html +++ b/public/index.html @@ -8,16 +8,37 @@ +
From 3ce78aae19ff925cb7e334a96bf711f6f0139916 Mon Sep 17 00:00:00 2001 From: Devin Brown Date: Sun, 23 Aug 2015 17:29:54 -0600 Subject: [PATCH 3/3] I'm too foo for this bar --- comments.json | 14 +++++++++++++- public/index.html | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/comments.json b/comments.json index 61f5ef60..f6dbe394 100644 --- a/comments.json +++ b/comments.json @@ -6,5 +6,17 @@ { "author": "Paul O’Shannessy", "text": "React is *great*!" + }, + { + "author": "asd", + "text": "asdas" + }, + { + "author": "asdas", + "text": "asda" + }, + { + "author": "asasa", + "text": "aaaaa" } -] +] \ No newline at end of file diff --git a/public/index.html b/public/index.html index 49aa49ed..ff3b839f 100644 --- a/public/index.html +++ b/public/index.html @@ -20,7 +20,24 @@ dataType: 'json', cache: false, success: function(data) { - this.setState({data: data}); + this.setState({data: data}); // update state! + }.bind(this), + error: function(xhr, status, err) { + console.error(this.props.url, status, err.toString()); + }.bind(this) + }); + }, + handleCommentSubmit: function(comment) { + var comments = this.state.data; + var newComments = comments.concat([comment]); + this.setState({data: newComments}); + $.ajax({ + url: this.props.url, + dataType: 'json', + type: 'POST', + data: comment, + success: function(data) { + this.setState({data: data}); // update state! }.bind(this), error: function(xhr, status, err) { console.error(this.props.url, status, err.toString()); @@ -39,7 +56,7 @@

Comments

- +
); } @@ -64,11 +81,25 @@

Comments

}); var CommentForm = React.createClass({ + handleSubmit: function(e) { + e.preventDefault(); + var author = React.findDOMNode(this.refs.author).value.trim(); + var text = React.findDOMNode(this.refs.text).value.trim(); + if (!text || !author) { + return; + } + this.props.onCommentSubmit({author: author, text: text}); + React.findDOMNode(this.refs.author).value = ''; + React.findDOMNode(this.refs.text).value = ''; + return; + }, render: function() { return ( -
- Hello, world! I am a CommentForm. -
+
+ + + +
); } });