em.rexhepi
em.rexhepi

Reputation: 289

Dynamic Route Map in meteorjs with iron-router

I'm trying to map routes dynamically. I have a document in the database with the categories: lie this.

{
  name : "menu" , content : 
    [
       {name : "Category-1", permalink : "cat-1", order : 0},
       {name : "Category-2", permalink : "cat-2", order : 0},
       {name : "Category-3", permalink : "cat-3", order : 0},
       {name : "Category-4", permalink : "cat-4", order : 0},
       {name : "Category-5", permalink : "cat-5", order : 0},
    ]
}

What i tired to do: client side:

Template.navBar.helpers({
menu  : function () {
    return Elements.findOne({name : "menu"}).content;
},
path : function () {
    permalink = this.permalink;
    Router.map(function () {
        this.route(permalink, {
            path: "/" + permalink + "/",
            template: 'listParent',
            data: function () {
                currentSubs(permalink, 'none', 0);
            }
        })
    });
    return "/" + permalink + "/";
}
});

Serverside startup:

//add menu to the router
var menu = Elements.findOne({name : "menu"}).content;
for (var i = 0; i < menu.length; i++) {
    console.log(menu[i].permalink);
    Router.map(function () {
        this.route(menu[i].permalink, {
            path: "/" + menu[i].permalink + "/",
            template: 'listParent'
        })
    });
}       

In the client side when I click in every route(on 5 of them) the results are like what i waite for Category-5.

Any idea on how can i achieve this without having to use:

this.route('list', {
    path: '/:list/',
    template: 'listParent',
    data: function () {
        list = this.params.list
        currentSubs(list, 'none', 0);
    }
});

I want a special route for every category that i have in menu document in database.

Upvotes: 1

Views: 199

Answers (1)

Ali Camarata
Ali Camarata

Reputation: 147

It wouldn't allow me to comment due to not having 50 reputation but I meant this as a comment above about the active routes and adding an active class to the <li>. Maybe this package is what you're looking for: https://atmospherejs.com/zimme/iron-router-active

Upvotes: 1

Related Questions