Search code examples
javascriptnode.jsmeteorroutesiron-router

Temporary Routes Using Meteor & Iron: Router?


Is it possible to make a temporary route in Meteor using Iron: Router?

How it'll be applied: When a user asks a question they will be redirected into a chatroom with a custom parameter that I supply. When the user is done in the chatroom and leaves, I want this route to be destroyed.

ex: /chat/[custom parameter I set]


Solution

  • Use : in your route to represent a value which can change but is mandatory (i.e. /chatroom/12345 would work but /chatroom would not)

    Router.route('/chatroom/:chatId', {
        name: 'chatroom',
        template: 'chatroom',
        layoutTemplate:"myLayout",
        data: function () {
            return ChatRoomData.findOne({_id:this.params.chatId});
        }
    });
    

    Or, you can make the parameter passed in optional by adding a ? too

    Router.route('/chatroom/:chatId?', {
        name: 'chatroom',
        template: 'chatroom',
        layoutTemplate:"myLayout",
        onBeforeAction: function () {
            if (!!this.params.chatId)
               //Do Something with the chatId
               this.render('specificChatRoom', {
                  data: function () {
                      return ChatRoomData.findOne({_id:this.params.chatId});
                  }
               });
            else
               //Do Something else
               this.render('mainChatRoom');
    
            this.next();
        }
    });