Reputation: 84
I'm just doing random things on Meteor to get a hang of it before I actually get into more serious things.
Anyway, I'm pretty sure there may be something wrong with the way I associate my helper function with the actual HTML on my project. Basically, I'm trying to make a simple Celsius to Fahrenheit/Fahrenheit to Celsius converter, where the # is converted as soon as a person enters a number. No button clicking involved.
Here's the HTML code:
<head>
<title>My Conversion Script</title>
</head>
<body>
<h1>My Conversion Script</h1>
{{ >converter}}
</body>
<template name="converter">
<b>Enter Celsius:</b>
<input type="text" name="celsius" {{userCalculation}}>
<b>Enter Fahrenheit:</b>
<input type="text" name="fahrenheit" {{userCalculation}}>
</template>
Here's the JS file:
if (Meteor.isClient) {
Template.converter.helpers({
'userCalculation': function() {
Session.get('celsiusCall');
Session.get('fahrenheitCall');
if (!NaN(celsius)) {
return celsius * 9/5 + 32;
} else if (!NaN(fahrenheit)) {
return (fahrenheit - 32) * 5/9;
}
}
});
Template.converter.events({
'keyup .celsius': function(e) {
var celsius = e.target.value;
Session.set('celsiusCall', celsius);
},
'keyup .fahrenheit': function(e) {
var fahrenheit = e.target.value;
Session.set('fahrenheitCall', fahrenheit);
}
});
}
I checked the event handling via console.log and the events work fine. I am making a mistake either the way I'm setting the sessions or the way I'm incorporating the helper function with the HTML itself.
Also, on a side note, am I correct to assume one session can only handle 1 variable?
Edit: The error I get is "Exception in template helper: ReferenceError: celsius is not defined"
Upvotes: 0
Views: 61
Reputation: 1401
You are correctly creating and receiving the session keys but you never assign them to the names of variables you are using. You would need to create variables for the Session.get calls like so:
var celsiusCall = Session.get('celsiusCall');
var fahrenheitCall = Session.get('fahrenheitCall');
Then you could use those vars like you currently are doing so now.
Upvotes: 0
Reputation: 4639
It's a variable scoping issue - the variables you define in your template events are not accessible in your template helpers, and vice-versa. So you defined var farenheit = e.target.value
in your event handler but farenheit
in your helper returns undefined. You'll want to define the variable in both.
Session works as a dictionary of key-value pairs so you can define multiple variables in it, if I understand your question correctly.
Session.set('first', 'a');
Session.set('second', 'b);
Session.keys // {first: 'a', second: 'b'}
Upvotes: 1