/
state-mixin.js
32 lines (27 loc) · 983 Bytes
/
state-mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';
var CHANGE_EVENT = require('./constants').CHANGE_EVENT;
module.exports = function(fluxPropName) {
return {
__updateState: function(Store) {
var state = {};
state[Store] = this.props[fluxPropName].Stores[Store].getState();
this.setState(state);
},
componentWillMount: function() {
for (var Store in this.props[fluxPropName].Stores) {
this.props[fluxPropName].Stores[Store].on(CHANGE_EVENT, this.__updateState.bind(this, Store));
}
},
componentWillUnmount: function() {
for (var Store in this.props.Stores) {
this.props[fluxPropName].Stores[Store].removeListener(CHANGE_EVENT, this.__updateState);
}
},
getInitialState: function() {
return Object.keys(this.props[fluxPropName].Stores).reduce(function(initialState, Store) {
initialState[Store] = this.props[fluxPropName].Stores[Store].getState();
return initialState;
}.bind(this), {});
}
}
};