diff --git a/README.md b/README.md index a6cc180..ebad82f 100644 --- a/README.md +++ b/README.md @@ -46,9 +46,22 @@ The list container accepts the following props: #### Basic Props -##### `collection` (object) [required] +##### `collection` (object|function) [required] + +The Meteor collection in which to look for data, or alternatively a function returning the collection or an Astronomy class. + +Example: -The Meteor collection in which to look for data. +```js +// meteor collection +collection = Categories; + +// passing an astronomy class +const Category = AstronomyClass.create({ + collection: Categories, +}); +collection = () => Category; +``` ##### `selector` (object) @@ -120,7 +133,7 @@ joins = [ }, { foreignProperty: "postId", - collection: Comments, + collection: () => Comments, joinAs: "comments" } ] diff --git a/lib/DocumentContainer.jsx b/lib/DocumentContainer.jsx index 5efe402..b13ed09 100644 --- a/lib/DocumentContainer.jsx +++ b/lib/DocumentContainer.jsx @@ -6,6 +6,10 @@ const DocumentContainer = React.createClass({ mixins: [ReactMeteorData], + getCollection(prop) { + return typeof prop === "function" ? prop() : prop; + }, + getMeteorData() { // subscribe if necessary @@ -14,7 +18,7 @@ const DocumentContainer = React.createClass({ const subscription = subscribeFunction(this.props.publication, this.props.terms); } - const collection = this.props.collection; + const collection = this.getCollection(this.props.collection); const document = collection.findOne(this.props.selector); // look for any specified joins @@ -37,7 +41,7 @@ const DocumentContainer = React.createClass({ // get the property containing the id or ids const joinProperty = document[join.localProperty]; - const collection = typeof join.collection === "function" ? join.collection() : join.collection; + const collection = this.getCollection(join.collection); // perform the join if (Array.isArray(joinProperty)) { // join property is an array of ids @@ -79,7 +83,10 @@ const DocumentContainer = React.createClass({ DocumentContainer.propTypes = { - collection: React.PropTypes.object.isRequired, + collection: React.PropTypes.oneOfType([ + React.PropTypes.object, + React.PropTypes.func + ]).isRequired, selector: React.PropTypes.object.isRequired, publication: React.PropTypes.string, terms: React.PropTypes.any, diff --git a/lib/ListContainer.jsx b/lib/ListContainer.jsx index 95650f0..a4aade5 100644 --- a/lib/ListContainer.jsx +++ b/lib/ListContainer.jsx @@ -15,6 +15,10 @@ const ListContainer = React.createClass({ }, mixins: [ReactMeteorData], + + getCollection(prop) { + return typeof prop === "function" ? prop() : prop; + }, getMeteorData() { @@ -45,7 +49,7 @@ const ListContainer = React.createClass({ const selector = this.props.selector || {}; const options = {...this.props.options, limit: this.state.limit}; - const cursor = this.props.collection.find(selector, options); + const cursor = this.getCollection(this.props.collection).find(selector, options); data.count = cursor.count(); @@ -60,7 +64,7 @@ const ListContainer = React.createClass({ // loop over each join this.props.joins.forEach(join => { - const collection = typeof join.collection === "function" ? join.collection() : join.collection; + const collection = this.getCollection(join.collection); const joinLimit = join.limit ? join.limit : 0; if (join.foreignProperty) { @@ -154,7 +158,10 @@ const ListContainer = React.createClass({ }); ListContainer.propTypes = { - collection: React.PropTypes.object.isRequired, // the collection to paginate + collection: React.PropTypes.oneOfType([ // the collection to paginate + React.PropTypes.object, + React.PropTypes.func + ]).isRequired, selector: React.PropTypes.object, // the selector used in collection.find() options: React.PropTypes.object, // the options used in collection.find() publication: React.PropTypes.string, // the publication to subscribe to