Skip to content

Instantly share code, notes, and snippets.

@nambrot
Last active August 29, 2015 14:19
Show Gist options
  • Save nambrot/633a910a4ac7ed63ce52 to your computer and use it in GitHub Desktop.
Save nambrot/633a910a4ac7ed63ce52 to your computer and use it in GitHub Desktop.
# components/posts/index.cjsx
Index = React.createClass
displayName: "PostsIndex"
render: ->
<div>
{
@props.posts.map (post) ->
<article key={post.get('id')}>
<header>
<h3>{post.get('title')}</h3>
</header>
</article>
.toArray()
}
</div>
FluxIndex = React.createClass
displayName: "WrappedPostsIndex"
render: ->
<FluxComponent connectToStores={
posts: (store) => posts: store.getAllPosts(), didFetchAll: store.didFetchAll()
}>
<Index />
</FluxComponent>
# actions/post.coffee
class PostActions extends Actions
fetchAllPosts: ->
axios.get('/posts', headers: acceptJSON)
.then (posts) -> posts.data
fetchPost: (id) ->
axios.get("/posts/#{id}", headers: acceptJSON)
.then (response) -> response.data
createPost: (post) ->
axios.post('/posts.json', post: post, headers: acceptJSON)
.then (response) -> response.data
updatePost: (post) ->
axios.put("/posts/#{post.id}", post: post, headers:acceptJSON)
.then (response) -> response.data
# stores/post.coffee
class PostsStore extends Store
constructor: (flux) ->
super()
postActionIds = flux.getActionIds('posts')
@registerAsync postActionIds.fetchAllPosts, @startFetchingPosts, @fetchedAllPosts
@registerAsync postActionIds.fetchPost, @startFetchingPost, @fetchedPost
@registerAsync postActionIds.createPost, @startCreatePost, @createdPost
@registerAsync postActionIds.updatePost, @startUpdatingPost, @updatedPost
@state = getDefaultState()
@flux = flux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment