Skip to content

Commit 0bf251e

Browse files
committed
day 2
1 parent e67591e commit 0bf251e

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

comments.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
{
33
"author": "Pete Hunt",
44
"text": "Hey there!",
5-
"likedCount":0
5+
"likedCount": 0
66
},
77
{
88
"author": "Paul O’Shannessy",
99
"text": "React is *great*!",
10-
"likedCount":15
10+
"likedCount": 15
1111
},
1212
{
1313
"author": "小明",
1414
"text": "我是晓明,come on baby。我是晓明,come on baby。我是晓明,come on baby。我是晓明,come on baby。",
15-
"likedCount":15
15+
"likedCount": 15
1616
}
1717
]

public/css/comment.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ button, html input[type="button"], input[type="reset"], input[type="submit"] {
7575
float: right;
7676
color: #faad4c;
7777
}
78+
.commentFooter .reply a{
79+
color: #faad4c;
80+
text-decoration: none;
81+
}
7882

7983
.commentFormHolder{
80-
display: none;
84+
/* display: none;*/
8185
position: fixed;
8286
top: 0;
8387
left: 0;
@@ -115,6 +119,8 @@ button, html input[type="button"], input[type="reset"], input[type="submit"] {
115119
background-color: #fff;
116120
}
117121
.commentBar .BtnOpenForm{
122+
display: inline-block;
123+
text-align: center;
118124
font-size: 1.1em;
119125
width: 60%;
120126
color: #fff;

public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<link rel="stylesheet" href="css/comment.css" />
1111
<script src="lib/react.js"></script>
1212
<script src="lib/JSXTransformer.js"></script>
13+
<script src="lib/ReactRouter.min.js"></script>
1314
<script src="lib/jquery.min.js"></script>
1415
<script src="lib/marked.min.js"></script>
1516
</head>

public/scripts/example.js

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var CommentFooter = React.createClass({
6969
return (
7070
<div className="commentFooter">
7171
<span className="time">25分钟前</span>
72-
<span className="reply">回复</span>
72+
<span className="reply"><a href='#/reply'>回复</a></span>
7373
</div>
7474
);
7575
}
@@ -117,24 +117,42 @@ var CommentBox = React.createClass({
117117
console.error(this.props.url, status, err.toString());
118118
}.bind(this)
119119
});
120+
window.location.hash='';
120121
},
121122
handleOpenForm:function(){
122-
$('.commentFormHolder').show();//.css('display','block');
123+
console.log('handleOpenForm');
124+
//$('.commentFormHolder').show();//.css('display','block');
125+
},
126+
handleHashChange:function() {console.log(window.location.hash.substr(1));
127+
this.setState({
128+
route: window.location.hash.substr(1)
129+
})
123130
},
124131
getInitialState: function() {
125-
return {data: []};
132+
return {
133+
data: [],
134+
route:window.location.hash.substr(1)
135+
};
126136
},
127137
componentDidMount: function() {
138+
window.addEventListener('hashchange', this.handleHashChange);
128139
this.loadCommentsFromServer();
129140
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
130141
},
131142
render: function() {
143+
144+
var CommentFormA;console.log('render',this.state.route);
145+
switch (this.state.route) {
146+
case '/post': CommentFormA = CommentForm; break;
147+
case '/reply': CommentFormA = CommentForm; break;
148+
default: CommentFormA = CommentFormBlank;
149+
}
132150
return (
133151
<div className="commentBox">
134152
<h1>Comments</h1>
135153
<CommentList data={this.state.data} />
136154
<CommentBar onOpenForm={this.handleOpenForm}/>
137-
<CommentForm onCommentSubmit={this.handleCommentSubmit} />
155+
<CommentFormA onCommentSubmit={this.handleCommentSubmit} />
138156
</div>
139157
);
140158
}
@@ -164,11 +182,16 @@ var CommentBar = React.createClass({
164182
render: function () {
165183
return (
166184
<div className="commentBar">
167-
<button className="BtnOpenForm" onClick={this.props.onOpenForm}>我也来说说</button>
185+
<a href='#/post' className="BtnOpenForm" onClick={this.props.onOpenForm}>我也来说说</a>
168186
</div>
169187
)
170188
}
171189
});
190+
var CommentFormBlank = React.createClass({
191+
render: function() {
192+
return (<br/>);
193+
}
194+
});
172195
var CommentForm = React.createClass({
173196
handleSubmit: function(e) {
174197
e.preventDefault();
@@ -177,11 +200,13 @@ var CommentForm = React.createClass({
177200
if (!text || !author) {
178201
return;
179202
}
180-
this.props.onCommentSubmit({author: author, text: text});
203+
this.props.onCommentSubmit({author: author, text: text,likedCount:0});
181204
React.findDOMNode(this.refs.author).value = '';
182205
React.findDOMNode(this.refs.text).value = '';
183206
},
184207
render: function() {
208+
/* var id = this.props.params.id;
209+
console.log('key=',id);*/
185210
return (
186211
<div className="commentFormHolder">
187212
<form className="commentForm" onSubmit={this.handleSubmit}>
@@ -198,3 +223,18 @@ React.render(
198223
<CommentBox url="comments.json" pollInterval={2000} />,
199224
document.getElementById('content')
200225
);
226+
227+
228+
/*var Router = ReactRouter.Router;
229+
var Route = ReactRouter.Route;
230+
var Link = ReactRouter.Link;
231+
232+
React.render((
233+
<Router>
234+
<Route path="/" component={CommentBox} >
235+
<Route path="post" component={CommentForm} />
236+
<Route path="reply/:id" component={CommentForm} />
237+
</Route>
238+
</Router>
239+
), document.getElementById('content')
240+
);*/

0 commit comments

Comments
 (0)