Adding events
To make our template a bit more dynamic, we will add a simple event, which will reactively rerun the logContext
helper we created earlier.
First, however, we need to add a button to our contextExample
template:
<button>Get some random number</button>
To catch the click event, open examples.js
and add the following event
function:
Template.contextExample.events({ 'click button': function(e, template){ Session.set('randomNumber', Math.random(0,99)); } });
This will set a session variable called randomNumber
to a random number.
Note
We will talk in depth about sessions in the next chapter. For now, we only need to know that when a session variable changes, all functions that get that session variable using Session.get('myVariable')
will run again.
To see this in action, we will add a Session.get()
call to the logContext
helper, and return the former set's random number as follows:
Template.contextExample.helpers({ logContext: function(){ console.log('Context Log...