-
Notifications
You must be signed in to change notification settings - Fork 45
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
How to count comments associated with referenceId? #83
Comments
I'm afraid my Meteor-fu is rusty these days. Your publications.js example references an undefined variable, I cloned your repo, but there are a few things that don't make sense to me here.
Also, I recommend reading about unique names and #43 for good measure. It appears you're trying to publish counts for multiple profiles on the same page. This will be a problem later when the page shows more than one profile. Think of your published count as a global variable that holds one value. Does it hold the review count for the first profile in the list, or the second, third, etc as each You may want to consider using a meteor call to fetch the counts from the server once. It seems a bit overkill to have the review counts updating in real-time, and may hurt performance (see kadira article link in #76). Here is a simple untested example: //// server.js
Meteor.methods({
getReviewCount: function (profileId) {
return Comments.getCollection().find({ referenceId: profileId }).count();
}
});
//// client.js
// hmm.... down the rabbit hole. Not sure where the list of profiles that are
// displayed comes from, so this isn't the optimal way to do it.
Profiles.find({}, { fields: { _id: 1 } }).fetch().forEach(function (profile) {
// provide a callback to Meteor.call so it doesn't block. Session will
// reactively update the page as the counts come in.
Meteor.call('getReviewCount', profile._id, function (count) {
Session.set('comments-' + profile._id, count);
});
});
//// template.js
Template.profiles.helpers({
reviewCount: function (id) {
return Session.get('comments-' + id);
},
});
//// template.html
<span class="label label-primary">
<i class="fa fa-rocket"></i>
{{reviewCount profile._id}} Reviews
</span> The upside is the counts are only computed once for this user, no observers are The downside is you've dumped a bunch of You may consider using a null collection on the client to store the counts I leave the exercise to you to figure out how to tap into the list of profiles Cheers |
@boxofrox Thanks for your prompt reply:
Does this clarify? I've been struggling for so long, really appreciate your help! Thanks. I tried your code, putting them in the respective areas. The console does not show any errors. However I'm unable to get any counts. I'm trying to get the "number of comments" to be displayed on "profileSmall" that will be displayed under the route "developers". And I think your solution would work! Just facing some issues implementing your solution now. |
Okay, I think I found the issue with publish-counts "not working". The problem is subscribing in //// /client/subscriptions.js
Meteor.subscribe('postCommentCount', "referenceId"); First, you subscribed with the string Second, you kind of subscribed too early. At this point you don't have a list of profiles to load counts against. Here's a very hackish alteration to //// /client/subscriptions.js
Meteor.subscribe("userData");
Meteor.subscribe("jobCount");
Meteor.subscribe("developerCount");
Meteor.subscribe('profiles', 0, /* callback when subscribe is ready */ function () {
// observe profiles and load review counts.
Profiles.find({}).observe({
added: function (doc) {
console.log('profile', doc);
Meteor.subscribe('postCommentCount', doc._id);
},
changed: function (oldDoc, newDoc) {
Meteor.subscribe('postCommentCount', newDoc._id);
},
});
}); I'll see if I can knock out a tested solution using my previous recommendation. |
Wow. I get what you mean. Your hackish solution works in the sense that there is a comment count now. But weirdly, the comment counts a single profile only. So I've a case where:
But both are displaying 2 reviews. This is one step in the right direction as I did not manage to even retrieve the counts previously. Thank you so much. |
That wierd part is related to the lack of unique names I mentioned previously. Here's an alternative solution. Apply it to your repo with cd repo
curl -L https://gist.github.com/boxofrox/9cf351660c29b8e94265e172d425ec6c/raw/392c37ee5d55c88dc1b110280b0fb27ba0df67b6/publish-counts-issue-83.diff | git apply It...
This code has much in common with publish-counts. The reasons I did not use publish-counts are...
Cons:
Note that there is probably a better solution than this long term. For Con 1, if you read the Kadira article I referenced in previous comment, that team recommends using For Con 2, you can use Footnote [1]: (see http://docs.meteor.com/#/full/observe)
|
@boxofrox You are amazing, really thanks :D |
@boxofrox I've seen your solutions here, it's amazing. Thanks for your help in advance. I'm still struggling to count comments attached to profiles.
I've profiles (same fork as weworkmeteor) where I use comments package (arkham) to attach comments to profiles.
Each comment seems to have a field "referenceId" which contains the profile's id to know which comments is attached to which. I'm trying to count the number of comments for each profile. Very simple case but I'm struggling with undefined errors.
publications.js
subscriptions.js
Meteor.subscribe('postCommentCount', referenceId);
*profileSmall.html *
{{getPublishedCount 'comments'}}
My repo forked from weworkmeteor shows the changes if you need more information.
The text was updated successfully, but these errors were encountered: