-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect-meteor-data.jsx
45 lines (39 loc) · 1.49 KB
/
connect-meteor-data.jsx
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
/**
* A React component composer function creator used to connect
* Meteor reactive data to any React component
* @param {function} getMeteorData - a function that returns as object
* with Meteor reactive data sources
* @return {function} - a function used to wrap React components
*/
connectMeteorData = function(getMeteorData) {
if (!getMeteorData || typeof getMeteorData !== 'function') {
throw new Error('You need to supply a function returning an ' +
'object of Meteor reactive data sources.');
}
/**
* Returns wrapped component connected with Meteor reactive
* data sources so that you don't have to access the data with
* `this.data` but rather with `this.props`.
* @param {React.Component} WrappedComponent - A React component
* @param {object} optional - an object with acceptable context types
* @return {React.Component} The resulting React component
*/
return function wrapReactComponent(WrappedComponent, contextTypes = {}) {
if (!WrappedComponent || !(WrappedComponent instanceof React.constructor)) {
throw new Error('You must pass a React component into this function.');
}
return React.createClass({
displayName: 'MeteorizedComponent',
mixins: [ReactMeteorData],
contextTypes: contextTypes,
getMeteorData() {
return getMeteorData.call(this, this.props, this.context);
},
render() {
return (
<WrappedComponent {...this.data} {...this.props}/>
);
}
});
};
};