Multi level category navigation - howto insert and select?

I’m making a Todo app/webservice but my knowledge to meteor and mongodb is still limited, normally i work with mySQL and PHP

I have made a todolist with categories but now i would like to add sub levels and when pushing the add + button it will insert a new category in my category collection with the parent it, if you are in a sub level.

1. howto get the parent Id from the url?

tasks/oKZnTPYFEZKLthEPx

and insert it into the parent_id field in my categories collection

2. how to make and if else that says:

if there is no Id in the url

/tasks

then show all categories where there is no parent_id else show where parent is tasks/_id

I use iron router/blaze/mongodb:

You aren’t really providing enough of your code for anyone to solve the problem.

Are you asking how to design your categories collection? There are plenty of ways, but two options would be either nest your tasks within the category, or create a tasks collection with a category field that references the parent category id, and an array of task ids within the category collection.

And if you mean how do you access the actual id from the url, with Iron router you should be able to grab the id by using Router.current().params._id (i believe that is the proper syntax, but if not just type Router.current() into your console and climb down the object until you find the id).

This is assuming you set up your route like so:

    name: 'task',
    template: 'task',
    waitOn(){ return Meteor.subscribe('task', this.params._id); },
    data () { return tasks.findOne(this.params._id); },
});```

Sorry for the missing info:

This is my mainLayout.helpers where i get the categories where user = userid :

Template.mainLayout.helpers({
    'cats': function(){

        return Categories.find({userid: Meteor.userId()}, { sort: { createdAt: -1 } });
    }

This is my route for tasks, I dont have a route for the mainLayout, do i need that?:


Router.route('/tasks/:_id', function() {

this.render('tasktemplate', {
    data: {
        appId: this.params._id
    }
});

The category list is in my nav drawer in the left side (all the time)

console.log(Router.current().params._id); worked

Now i only need to make a if else that tjeck what to find in the collection

if no parentId then
else Categories.find where parent = thisurl_catId

if (typeof thisurl_catId === 'undefined' || thisurl_catId === null) {
                            // variable is undefined or null
                            return Categories.find({userid: Meteor.userId()}, { sort: { createdAt: -1 } });
                        }
                        else
                        {
                             return Categories.find({parentId: thisurl_catId, userid: Meteor.userId()}, { sort: { createdAt: -1 } });
                        }

Do it look okay? it works

I am not going to walk you through how to build your app, but from what you posted it looks like there should be two separate routes, and two separate templates.

Make a /tasks route that points to a general tasks feed instead of checking if there is an id in the route and using the same template.

Be sure to read through guide.meteor.com. The section on URLs and routes is very helpful.

1 Like