Reputation: 4320
I need to access a Session Variable from a RouteController in the waitOn
function in my meteor application, I set a session variable on the onCreated
block in a template:
Template.practicalQuestionForm.onCreated ->
Session.set 'domId', Random.id()
Then I need to access to that Session variable Session.get 'domId'
from my controller, look at the waitOn
:
@testsAddQuestionController = testsQuestionsController.extend
template: ->
qType = Router.current().params.type
if qType == 'practical'
'practicalQuestionForm'
else if qType == 'mcq'
'mcqQuestionForm'
waitOn: ->
console.log Session.get 'domId'
Meteor.subscribe 'currentSessionUploads', Session.get 'domId'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
previous: TestQuestions.find({}, sort: createdAt: 1, limit: 1).fetch().pop()
However I only get undefined
can someone tell me if this is possible? if not what other alternatives do you can advise to me?
Thanks in advance.
Upvotes: 2
Views: 167
Reputation: 5156
If you want to use Session
in your waitOn
function, you need to make sure that this code will be executed on the client.
For instance:
waitOn: function() {
var domId = undefined;
if(Meteor.isClient) {
domId = Session.get('domId');
}
return Meteor.subscribe('currentSessionUploads', domId);
}
Please note that you need to check if domId
is undefined in your publication (server-side).
Also You have to check that your Session
variable is not already defined, otherwise you will get an infinite loop and your controller will get crazy:
Template.practicalQuestionForm.onCreated ->
if not Session.get 'domId'
Session.set 'domId', Random.id()
Upvotes: 2