I have a collection, lets say Authors and every Author has many Books (separate collection). So as part of my Authors SimpleSchema config I have:
bookId: {
type: Array
}
And within that array I have all the book ID’s belonging to the specific author:
["id_1","id_2","id_3","id_4","id_5"]
So my question is, how can I create a Author.books method (think Rails) that would return all my book objects (not just the ID’s) of the given author?
I think there might be a few packages that can do this (please feel free to suggest) but I would love to know how to do it manually as a great learning experience.
So I am having a small issue now. When I go to /author/_id/books the books will not render unless I manually refresh the page. I am using template level subscriptions as follows:
Template.authorBooks.onCreated(function () {
var self = this;
self.subscribe('authors');
self.subscribe('books');
});
In my publications.js I have:
Meteor.publish('authors', function () {
return Authors.find();
});
Meteor.publish('books', function () {
return Books.find();
});
I am a little confused as to why it only works on a manual page reload, if I am browsing my /authors page and click an author, the books will not load until I manually refresh the page.
I don’t know if it makes a difference, but maybe move it to /lib/router.js. If the books come up on a manual refresh (server route) but not just surfing to it (client route), maybe it’s related to the routes not being loaded before everything else?
In your template, are you waiting till the subscriptions are ready before showing the content? e.g.:
{{#if Template.subscriptionsReady}}
<!-- show books -->
{{/if}}
Glad it worked! You could also, of course, use an {{else}} block to show a spinner or some “loading…” text or whatever. Very handy. Template based subscriptions are awesome!