-
-
Save klebershimabuku/35dc3192188207493081 to your computer and use it in GitHub Desktop.
React.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsx React.DOM */ | |
var Posts = React.createClass({ | |
render: function() { | |
return ( | |
<div className="posts"> | |
<h3>Featured Jobs</h3> | |
<PostList /> | |
<p>More Awesome Jobs →</p> | |
</div> | |
) | |
} | |
}); | |
var PostList = React.createClass({ | |
getInitialState: function() { | |
return { posts: [] }; | |
}, | |
componentWillMount: function() { | |
$.getJSON('/api/posts', function(results) { | |
this.setState({ | |
posts: results | |
}); | |
}.bind(this)); | |
}, | |
render: function() { | |
var posts = this.state.posts.map(function(post) { | |
return <PostListItem key={post.id} post={post} />; | |
}); | |
return <ul className='post-list'>{posts}</ul>; | |
} | |
}); | |
var PostListItem = React.createClass({ | |
getInitialState: function() { | |
return { post: null } | |
}, | |
handleClick: function(e) { | |
var post_id = this.props.post.id; | |
var path = '/api/posts/' + post_id; | |
$.getJSON(path, function(post) { | |
this.setState({ post: post }); | |
}.bind(this)); | |
}, | |
render: function() { | |
if (this.state.post) { | |
return(<Post data={this.state.post} />) | |
} else { | |
return ( | |
// Actually it is being rendered inside the PostList component. | |
// I want to render it like it a new page, just with the PageHeader, Post and Footer components. | |
<div className="post-item"> | |
<li><a href="#" onClick={this.handleClick}>{this.props.post.title}</a></li> | |
</div> | |
); | |
} | |
} | |
}); | |
var Post = React.createClass({ | |
render: function() { | |
var post = this.props.data; | |
return( | |
<div className="post"> | |
<h1>{post.title}</h1> | |
<h2>{post.location}</h2> | |
<p>{post.description}</p> | |
</div> | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment