using terms and params for commentsList subscription

This commit is contained in:
Sacha Greif 2015-05-08 09:33:27 +09:00
parent 11bd064039
commit b60074ea53
6 changed files with 41 additions and 39 deletions

View file

@ -1,4 +1,4 @@
-<template name="comments_list_compact">
<template name="comments_list_compact">
<table>
<thead>
<tr>

View file

@ -14,7 +14,7 @@ Template.commentsListController.onCreated(function () {
// initialize the reactive variables
instance.terms = new ReactiveVar(instance.data.terms);
instance.commentsLimit = new ReactiveVar(Settings.get('commentsPerPage', 10));
instance.commentsLimit = new ReactiveVar(Settings.get('commentsPerPage', 5));
// 2. Autorun
@ -22,7 +22,7 @@ Template.commentsListController.onCreated(function () {
instance.autorun(function () {
// add a dependency on data context to trigger the autorun
var terms = Template.currentData().terms; // ⚡ reactive ⚡
instance.commentsLimit.set(Settings.get('commentsPerPage', 10));
instance.commentsLimit.set(Settings.get('commentsPerPage', 5));
});
// Autorun 2: will re-run when limit or terms are changed
@ -38,14 +38,14 @@ Template.commentsListController.onCreated(function () {
var subscriptionTerms = _.extend(_.clone(terms), {limit: commentsLimit}); // extend terms with limit
// use this new object to subscribe
var commentsSubscription = instance.subscribe('userComments', subscriptionTerms);
var commentsSubscription = instance.subscribe('commentsList', subscriptionTerms);
var subscriptionsReady = instance.subscriptionsReady(); // ⚡ reactive ⚡
console.log('// ------ autorun running ------ //');
console.log("terms: ", terms);
console.log("limit: ", commentsLimit);
console.log("ready: ", subscriptionsReady);
// console.log('// ------ autorun running ------ //');
// console.log("terms: ", terms);
// console.log("limit: ", commentsLimit);
// console.log("ready: ", subscriptionsReady);
// Tracker.onInvalidate(console.trace.bind(console));
// if subscriptions are ready, set terms to subscriptionsTerms
@ -91,7 +91,7 @@ Template.commentsListController.helpers({
// increase limit by 5 and update it
var limit = instance.commentsLimit.get();
limit += Settings.get('commentsPerPage', 10);
limit += Settings.get('commentsPerPage', 5);
instance.commentsLimit.set(limit);
},
@ -103,7 +103,7 @@ Template.commentsListController.helpers({
};
console.log(data)
// console.log(data)
return data;
}
});

View file

@ -1,5 +1,22 @@
Comments._ensureIndex({"postId": 1});
// Publish a list of comments
Meteor.publish('commentsList', function(terms) {
if(Users.can.viewById(this.userId)){
var parameters = Comments.getSubParams(terms);
var comments = Comments.find(parameters.find, parameters.options);
// if there are comments, find out which posts were commented on
var commentedPostIds = comments.count() ? _.pluck(comments.fetch(), 'postId') : [];
return [
comments,
Posts.find({_id: {$in: commentedPostIds}})
];
}
});
// Publish a single comment
Meteor.publish('singleCommentAndChildren', function(commentId) {
@ -23,22 +40,6 @@ Meteor.publish('commentPost', function(commentId) {
return [];
});
// Publish a user's comments and the posts that were commented on
Meteor.publish('userComments', function(terms) {
if(Users.can.viewById(this.userId)){
var parameters = Comments.getSubParams(terms);
var comments = Comments.find(parameters.find, parameters.options);
// if there are comments, find out which posts were commented on
var commentedPostIds = comments.count() ? _.pluck(comments.fetch(), 'postId') : [];
return [
comments,
Posts.find({_id: {$in: commentedPostIds}})
];
}
});
// Publish author of the current comment, and author of the post related to the current comment
Meteor.publish('commentUsers', function(commentId) {
@ -63,13 +64,4 @@ Meteor.publish('commentUsers', function(commentId) {
return [];
});
// Publish comments for a specific post
Meteor.publish('postComments', function(postId) {
if (Users.can.viewById(this.userId)){
return Comments.find({postId: postId}, {sort: {score: -1, postedAt: -1}});
}
return [];
});

View file

@ -22,12 +22,14 @@ Comments.views.baseParameters = {
Comments.views.register("postComments", function (terms) {
return {
find: {postId: terms.postId}
find: {postId: terms.postId},
options: {sort: {score: -1, postedAt: -1}}
};
});
Comments.views.register("userComments", function (terms) {
return {
find: {userId: terms.userId}
find: {userId: terms.userId},
options: {sort: {postedAt: -1}}
};
});

View file

@ -20,6 +20,8 @@ Package.onUse(function (api) {
'lib/comments.js',
'lib/methods.js',
'lib/callbacks.js',
'lib/views.js',
'lib/parameters.js',
'lib/routes.js'
], ['client', 'server']);
@ -33,7 +35,13 @@ Package.onUse(function (api) {
'lib/client/templates/comment_list.html',
'lib/client/templates/comment_list.js',
'lib/client/templates/comment_reply.html',
'lib/client/templates/comment_reply.js'
'lib/client/templates/comment_reply.js',
'lib/client/templates/comments_list/comments_list.html',
'lib/client/templates/comments_list/comments_list.js',
'lib/client/templates/comments_list/comments_list_compact.html',
'lib/client/templates/comments_list/comments_list_compact.js',
'lib/client/templates/comments_list/comments_list_controller.html',
'lib/client/templates/comments_list/comments_list_controller.js',
], ['client']);
api.addFiles([

View file

@ -92,7 +92,7 @@ Posts.controllers.page = RouteController.extend({
waitOn: function() {
this.postSubscription = coreSubscriptions.subscribe('singlePost', this.params._id);
this.postUsersSubscription = coreSubscriptions.subscribe('postUsers', this.params._id);
this.commentSubscription = coreSubscriptions.subscribe('postComments', this.params._id);
this.commentSubscription = coreSubscriptions.subscribe('commentsList', {view: 'postComments', postId: this.params._id});
},
post: function() {