Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iron router: how to set session using this.params._id

I have a route /scoreboard/1 where 1 is not a restful id of an object but a court id which I want to use to select games where court.

I have tried this code which sets the session but doesn't load the template. The template loads normally if I remove the event and hard code var court.

this.route('scoreboard',
    {

    path: '/scoreboard/:_id',

    template: 'scoreboard',

    onBeforeAction: function () {
       Session.set('court', this.params._id);
    }

  }); //route

I have found that this seems to work. What isn't working is this:

var court = Session.get("court");

console.log(court); -> 1

myGame = Games.findOne({court_id: court});

while this works:

myGame = Games.findOne({court_id: 1});

found it!

  var court = parseInt(Session.get('court'));
like image 969
markhorrocks Avatar asked Dec 25 '22 03:12

markhorrocks


1 Answers

This works for me:

$ meteor create test
$ cd test
$ meteor add iron:router

test.js:

if (Meteor.isClient) {
    Template.scoreboard.id = function() {
        return Session.get('court');
    }
}

Router.route('/scoreboard/:_id', {
    name: 'scoreboard',
    path: '/scoreboard/:_id',
    template: 'scoreboard',
    onBeforeAction: function () {
       Session.set('court', this.params._id);
    }
});

test.html:

<head>
</head>

<template name="scoreboard">
  Scoreboard<br/>
  id is: {{id}}
</template>

Then go to localhost:3000/scoreboard/123.

like image 187
Christian Fritz Avatar answered Jan 19 '23 02:01

Christian Fritz