guillaumek guillaumek - 2 months ago 15
Javascript Question

Meteor: Count the number of collection item in relational collection

I'm doing a lists/tasks application, I can select the list who display me the good tasks using a session to store which list I'm clicking.
Using the Meteor task board tutorial, I can count how items I have.
the problem is the count system is not related to all lists but to only the list I'm.

I can't understand how to count the items for each lists, and display the counter in front of each lists item.
in the tutorial, the counter is in Session, so do I have to create a new "counter" in the collection, or can I keep the session ?

Some code:
List.js

if (Meteor.isServer) {
Meteor.publish('lists', function listsPublication() {
return Lists.find({
$or: [
{private: { $ne: true}},
{owner: this.userId},
],
});
});
}

Meteor.methods({

Template.list.helpers({
incompleteCount() {
return Tasks.find({ listId: Session.get('listId'), checked: { $ne: true } }).count();
},

Template.list.events({
'click .list-selected' (event) {
event.preventDefault();

var list = $(event.currentTarget).attr('list-id');
Session.set('listId', list);
},


Body.js

Template.body.helpers({
tasks() {
const instance = Template.instance();

if (instance.state.get('hideCompleted')) {
return Tasks.find({
listId: Session.get('listId'),
checked: { $ne: true },
}, { sort: Session.get("sort_order") });
}
return Tasks.find({listId: Session.get('listId')}, { sort: Session.get("sort_order")});
},

lists() {
return Lists.find({}, { sort: { createdAt: -1 } });
},

});


Thanks in advance

Answer

Thanks @CodeChimp, I could find the way just using a param in my helper:

    incompleteCount(listId) {
        return Tasks.find({ listId: listId, checked: { $ne: true } }).count();
    },

And the template (using jade):

   .ui.teal.left.pointing.label
        span {{incompleteCount this._id}}

Using the search I did before in one list, but I just added the param instead of the session.get() who just get the active list ...

Thanks

Comments