Skip to content

Commit 136aa1f

Browse files
committed
Flux - 13 - Triggering Changes From a Store
1 parent f114d82 commit 136aa1f

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
var React = require('react');
2+
var Reflux = require('reflux');
23
var TopicStore = require('../stores/topic-store');
34

45
module.exports = React.createClass({
6+
mixins: [
7+
Reflux.listenTo(TopicStore, 'onChange')
8+
],
59
getInitialState: function() {
610
return {
711
topics: []
812
}
913
},
1014
componentWillMount: function() {
11-
TopicStore.getTopics()
12-
.then(function(){
13-
// We have successfully fetched topics
14-
// topics are available on TopicStore.topics
15-
this.setState({
16-
topics: TopicStore.topics
17-
});
18-
}.bind(this));
15+
TopicStore.getTopics();
1916
},
2017
render: function() {
2118
return <div className="list-group">
@@ -29,5 +26,8 @@ module.exports = React.createClass({
2926
{topic}
3027
</li>
3128
});
29+
},
30+
onChange: function(event, topics) {
31+
this.setState({topics: topics});
3232
}
3333
});

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module.exports = Reflux.createStore({
66
return Api.get('topics/defaults')
77
.then(function(json){
88
this.topics = json.data;
9+
this.triggerChange();
910
}.bind(this));
11+
},
12+
triggerChange: function() {
13+
this.trigger('change', this.topics);
1014
}
1115
});

0 commit comments

Comments
 (0)