- Building Single:page Web Apps with Meteor
- Fabian Vogelsteller
- 239字
- 2021-08-06 19:29:38
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.
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 Helper',this);
return Session.get('randomNumber');
}
});
If we go to the browser, we will see the Get some random number button. When we click on it, we see a random number appearing just above the button.
Now that we have covered template helpers, let's create a custom block helper.