2016-02-16 15:40:37 +09:00
|
|
|
// 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,
|
2016-02-22 13:23:46 +09:00
|
|
|
terms: React.PropTypes.object,
|
|
|
|
joins: React.PropTypes.array
|
2016-02-16 15:40:37 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-02-18 11:55:00 +09:00
|
|
|
const collection = this.props.collection;
|
2016-02-23 11:34:40 +09:00
|
|
|
const document = collection.findOne(this.props.terms);
|
|
|
|
|
2016-02-22 13:23:46 +09:00
|
|
|
// 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];
|
2016-02-22 13:23:46 +09:00
|
|
|
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();
|
2016-02-22 13:23:46 +09:00
|
|
|
} else { // join property is a single id
|
2016-02-23 11:34:40 +09:00
|
|
|
document[join.joinAs] = collection.findOne({_id: joinProperty});
|
2016-02-22 13:23:46 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2016-02-16 15:40:37 +09:00
|
|
|
|
|
|
|
return {
|
2016-02-23 11:34:40 +09:00
|
|
|
document: document,
|
2016-02-18 16:26:52 +09:00
|
|
|
currentUser: Meteor.user()
|
2016-02-16 15:40:37 +09:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
const Component = this.props.component; // could be Post or PostEdit
|
|
|
|
|
2016-02-23 11:34:40 +09:00
|
|
|
if (this.data.document) {
|
2016-02-16 15:40:37 +09:00
|
|
|
return (
|
2016-02-23 11:34:40 +09:00
|
|
|
<Component {...this.data} />
|
2016-02-16 15:40:37 +09:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return <p>Loading…</p>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = ItemContainer;
|