From 58e3c9d95ebb1a945aadc53b9a91724641278cde Mon Sep 17 00:00:00 2001 From: ochicf Date: Thu, 6 Oct 2016 13:34:43 +0200 Subject: [PATCH 1/4] added getCollection method --- lib/DocumentContainer.jsx | 4 ++++ lib/ListContainer.jsx | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lib/DocumentContainer.jsx b/lib/DocumentContainer.jsx index 5efe402..a4308bf 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 diff --git a/lib/ListContainer.jsx b/lib/ListContainer.jsx index 95650f0..3ed0b30 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() { From a06210ad33ce7a280b84b8722b6f121c6872ff13 Mon Sep 17 00:00:00 2001 From: ochicf Date: Thu, 6 Oct 2016 13:35:12 +0200 Subject: [PATCH 2/4] accept function as collection prop --- lib/DocumentContainer.jsx | 5 ++++- lib/ListContainer.jsx | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/DocumentContainer.jsx b/lib/DocumentContainer.jsx index a4308bf..0618ba6 100644 --- a/lib/DocumentContainer.jsx +++ b/lib/DocumentContainer.jsx @@ -83,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 3ed0b30..6c40722 100644 --- a/lib/ListContainer.jsx +++ b/lib/ListContainer.jsx @@ -158,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 From 2a1e57568769b701f34a7c6e50cf0c188864e3dd Mon Sep 17 00:00:00 2001 From: ochicf Date: Thu, 6 Oct 2016 13:39:22 +0200 Subject: [PATCH 3/4] use getCollection method --- lib/DocumentContainer.jsx | 4 ++-- lib/ListContainer.jsx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/DocumentContainer.jsx b/lib/DocumentContainer.jsx index 0618ba6..b13ed09 100644 --- a/lib/DocumentContainer.jsx +++ b/lib/DocumentContainer.jsx @@ -18,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 @@ -41,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 diff --git a/lib/ListContainer.jsx b/lib/ListContainer.jsx index 6c40722..a4aade5 100644 --- a/lib/ListContainer.jsx +++ b/lib/ListContainer.jsx @@ -49,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(); @@ -64,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) { From dbd4f121d1bdad5c2924dae2c882f61161163722 Mon Sep 17 00:00:00 2001 From: ochicf Date: Thu, 6 Oct 2016 13:54:01 +0200 Subject: [PATCH 4/4] updated readme --- README.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) 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" } ]