Vulcan/packages/nova-core/lib/containers/ItemContainer.jsx

67 lines
1.7 KiB
React
Raw Normal View History

// import React from 'react';
const ItemContainer = React.createClass({
propTypes: {
collection: React.PropTypes.object.isRequired,
component: React.PropTypes.func.isRequired,
2016-02-16 16:12:13 +09:00
publication: React.PropTypes.string.isRequired,
terms: React.PropTypes.object,
joins: React.PropTypes.array
},
mixins: [ReactMeteorData],
getMeteorData() {
2016-02-23 11:34:40 +09:00
// subscribe if necessary
if (this.props.publication && this.props.terms) {
const subscription = Meteor.subscribe(this.props.publication, this.props.terms);
}
const collection = this.props.collection;
2016-02-23 11:34:40 +09:00
const document = collection.findOne(this.props.terms);
// look for any specified joins
if (this.props.joins) {
// loop over each join
this.props.joins.forEach(join => {
// get the property containing the id or ids
2016-02-23 11:34:40 +09:00
const joinProperty = document[join.property];
const collection = Meteor.isClient ? window[join.collection] : global[join.collection];
// perform the join
if (Array.isArray(joinProperty)) { // join property is an array of ids
2016-02-23 11:34:40 +09:00
document[join.joinAs] = collection.find({_id: {$in: joinProperty}}).fetch();
} else { // join property is a single id
2016-02-23 11:34:40 +09:00
document[join.joinAs] = collection.findOne({_id: joinProperty});
}
});
}
return {
2016-02-23 11:34:40 +09:00
document: document,
2016-02-18 16:26:52 +09:00
currentUser: Meteor.user()
};
},
render() {
const Component = this.props.component; // could be Post or PostEdit
2016-02-23 11:34:40 +09:00
if (this.data.document) {
return (
2016-02-23 11:34:40 +09:00
<Component {...this.data} />
)
} else {
return <p>Loading</p>
}
}
});
module.exports = ItemContainer;