Skip to content
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

Collections in grouper object #12

Closed
aldeed opened this issue Oct 4, 2014 · 4 comments
Closed

Collections in grouper object #12

aldeed opened this issue Oct 4, 2014 · 4 comments

Comments

@aldeed
Copy link
Contributor

aldeed commented Oct 4, 2014

I usually group my collections in an object:

Collections = {
  Posts: new Mongo.Collection("posts")
};

This doesn't seem to be supported since you use the property name as the collection. Maybe just allow an override:

collections: {
  Posts: {
    collectionObject: Collections.Posts
  }
}

(Trying out the package on a new project. Will probably submit lots of issues, but I can try to help with PRs when I have time. Seems really good so far.)

@yogiben
Copy link
Owner

yogiben commented Oct 6, 2014

Thanks for your first PR. I will implement this tomorrow.

Out of interest, what's the benefit of grouping your collections?

@aldeed
Copy link
Contributor Author

aldeed commented Oct 6, 2014

It keeps the window object less cluttered on the client, but I only do it in really large apps, and it's not really critical. It's also useful when you're creating the collection in a package and you want to attach everything to a single exported variable. Similar to how Meteor.users is attached to Meteor instead of just Users. You handle the users collection specially, but another package could export in the same way.

@yogiben
Copy link
Owner

yogiben commented Oct 13, 2014

@aldeed Could you share the snippet or method you use for parsing strings with '.' to return JS objects?

@aldeed
Copy link
Contributor Author

aldeed commented Oct 13, 2014

function lookup(obj) {
    var ref = window;
    var arr;
    if (typeof obj === "string") {
      arr = obj.split(".");
      while(arr.length && (ref = ref[arr.shift()]));
      if (!ref) {
        throw new Error(obj + " is not in the window scope");
      }
      return ref;
    }
    return obj;
  }

That takes either the object itself or a string representation of it with dots. If it's an object, just returns it, otherwise lookup based on the string pieces. It's client-specific, though, so if used in server code, you'd have to detect whether client or server, and change that first line to ref = global (I think) on the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants