angularjs - Meteor reactive sources -
i have app there 5 collections: 1 books, 1 types (of books, fantasy, sci-fi...), 1 languages, 1 subscribtions (including type_id, lang_id , user_id) , last 1 if meteor.users.
i created helper function retrieve books fitting subscribtions of each user (i'm using angular-meteor)
$scope.helpers({ userbooks:function(){ var subbooks=[]; var books; var user_id=meteor.userid(); subscribtions.find({user_id:user_id}).foreach(function(sub){ books=books.find({type_id:sub.type_id,lang_id:sub.lang_id}).fetch(); for(var i=0;i<books.length;i++){ subbooks.push(books[i]); } }); return subbooks; } })
suppose new book or new subscribtions added, or user logs it, subbooks data updates? considered reactive (because contains reactive data)?
i suggest simplify , publish joined collection of subscriptions
, books
server using reywood:publish-composite package.
server:
meteor.publishcomposite('mysubs', { find: function() { // find subscriptions current user return subscriptions.find({ userid: this.userid }); }, children: [ { find: function(sub) { // find books related current subscription 'sub' return books.find({ type_id: sub.type_id, lang_id: sub.lang_id}); } } ] });
on client:
meteor.subscribe('mysubs');
then can find books current user interested in books.find()
this make app faster reducing number of subscriptions , books sending server client.
note: on time might find confusing name collection subscriptions since term core meteor concept.
Comments
Post a Comment