Skip to content

Commit

Permalink
Flux - 28 - Fetching Single Records from a Store Continued
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenGrider committed Jul 5, 2015
1 parent 2528ac5 commit b573cb7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion imgur-client/src/actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ var Reflux = require('reflux');

module.exports = Reflux.createActions([
'getTopics',
'getImages'
'getImages',
'getImage'
]);
15 changes: 12 additions & 3 deletions imgur-client/src/components/image-detail.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
var React = require('react');
var Reflux = require('reflux');
var ImageStore = require('../stores/image-store');
var Actions = require('../actions');

module.exports = React.createClass({
mixins: [
Reflux.listenTo(ImageStore, 'onChange')
],
getInitialState: function() {
return {
image: null
}
},
componentWillMount: function() {
Actions.getImage(this.props.params.id);
},
render: function() {
return <div>
I am an image detail.
{this.state.image}
</div>
},
onChange: function(event, image) {
onChange: function() {
this.setState({
image: image
image: ImageStore.find(this.props.params.id)
});
}
});
12 changes: 12 additions & 0 deletions imgur-client/src/stores/image-store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ module.exports = Reflux.createStore({
this.triggerChange();
}.bind(this));
},
getImage: function(id) {
Api.get('gallery/image/' + id)
.then(function(json){
if(this.images){
this.images.push(json.data);
} else {
this.images = [json.data];
}

this.triggerChange();
}.bind(this));
},
find: function(id){
var image = _.findWhere(this.images, {id: id});

Expand Down

0 comments on commit b573cb7

Please sign in to comment.