errata
errata

Reputation: 6031

Creating routes dynamically in Meteor

I'm using meteor-router package to set up routes in my app. However, I need to be able to set up some routes "dynamically". It means that I have some "static" routes and some "dynamic", which are changing depending on some variable (specifically part of URL).
For example, if user visits www.example.com/foo my routes should prefix all URLs with foo, if user visits www.example.com/bar, all routes should have bar prefix.

Pseudo code:

Meteor.subscribe('bar', function(){
  var prefix = window.location.pathname.replace(/^\/([^\/]*).*$/, '$1');

  // "dynamic" routes, generated after 'subscribe' is ready
  Meteor.Router.add({
    prefix+'/': function() {
      // some code
      return 'mainTemplate';
    },
    prefix+'/welcome': 'welcome',
    prefix+'/foo': 'foo',
    prefix+'/bar': 'bar'
  });
)};

// "static" routes, independent of current URL
Meteor.Router.add({
  '/': 'home',
  '/admin': 'admin'
});

Upvotes: 1

Views: 703

Answers (1)

Tarang
Tarang

Reputation: 75945

You're above set up should work, only change each route to have the / prefix

Routes = {}

Routes['/' + prefix + '/welcome'] = function() { return 'welcome' };
Routes['/' + prefix +' /foo'] = 'foo';
...

Meteor.Router.add(Routes);

Upvotes: 1

Related Questions