Skip to content

Commit b573cb7

Browse files
committed
Flux - 28 - Fetching Single Records from a Store Continued
1 parent 2528ac5 commit b573cb7

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

imgur-client/src/actions.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ var Reflux = require('reflux');
22

33
module.exports = Reflux.createActions([
44
'getTopics',
5-
'getImages'
5+
'getImages',
6+
'getImage'
67
]);
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
var React = require('react');
22
var Reflux = require('reflux');
33
var ImageStore = require('../stores/image-store');
4+
var Actions = require('../actions');
45

56
module.exports = React.createClass({
67
mixins: [
78
Reflux.listenTo(ImageStore, 'onChange')
89
],
10+
getInitialState: function() {
11+
return {
12+
image: null
13+
}
14+
},
15+
componentWillMount: function() {
16+
Actions.getImage(this.props.params.id);
17+
},
918
render: function() {
1019
return <div>
11-
I am an image detail.
20+
{this.state.image}
1221
</div>
1322
},
14-
onChange: function(event, image) {
23+
onChange: function() {
1524
this.setState({
16-
image: image
25+
image: ImageStore.find(this.props.params.id)
1726
});
1827
}
1928
});

imgur-client/src/stores/image-store.jsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ module.exports = Reflux.createStore({
1515
this.triggerChange();
1616
}.bind(this));
1717
},
18+
getImage: function(id) {
19+
Api.get('gallery/image/' + id)
20+
.then(function(json){
21+
if(this.images){
22+
this.images.push(json.data);
23+
} else {
24+
this.images = [json.data];
25+
}
26+
27+
this.triggerChange();
28+
}.bind(this));
29+
},
1830
find: function(id){
1931
var image = _.findWhere(this.images, {id: id});
2032

0 commit comments

Comments
 (0)