This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (45 loc) · 1.55 KB
/
index.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var React = require('react');
var connectToStores = require('flummox/connect');
if (connectToStores.default && !(connectToStores instanceof Function)) {
connectToStores = connectToStores.default;
}
/**
* Adds flux context if someone forgot it:
*/
function addFluxContext(component) {
if (component.stores || component.actions) {
component.contextTypes = component.contextTypes || {};
component.contextTypes.flux = component.contextTypes.flux || React.PropTypes.object;
}
return component;
}
/**
* Attaches Flummox actions to `this.actions` before component mounts.
* {... actions: ['example'], ...} turns into: `this.actions.example`.
*/
function addActions(component) {
var oldActions = component.actions;
var oldWillMount = component.componentWillMount;
if (oldActions) {
component.componentWillMount = function() {
this.actions = {};
for (var i = 0; i < oldActions.length; i++) {
var name = oldActions[i];
this.actions[name] = this.context.flux.getActions(name);
}
return oldWillMount && oldWillMount.apply(this, arguments);
};
}
return component;
}
/**
* Automatically connects React component to Flummox stores and actions.
*/
module.exports = function(displayName, innerComponent) {
//set displayName for better debugging
innerComponent.displayName = displayName;
return connectToStores(
React.createClass(addFluxContext(addActions(innerComponent))),
innerComponent.stores
);
};