Skip to content

Astronomy support #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -120,7 +133,7 @@ joins = [
},
{
foreignProperty: "postId",
collection: Comments,
collection: () => Comments,
joinAs: "comments"
}
]
Expand Down
13 changes: 10 additions & 3 deletions lib/DocumentContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const DocumentContainer = React.createClass({

mixins: [ReactMeteorData],

getCollection(prop) {
return typeof prop === "function" ? prop() : prop;
},

getMeteorData() {

// subscribe if necessary
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 10 additions & 3 deletions lib/ListContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const ListContainer = React.createClass({
},

mixins: [ReactMeteorData],

getCollection(prop) {
return typeof prop === "function" ? prop() : prop;
},

getMeteorData() {

Expand Down Expand Up @@ -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();

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down