- 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.
推薦閱讀
- Google Apps Script for Beginners
- Kibana Essentials
- 單片機C語言程序設計實訓100例:基于STC8051+Proteus仿真與實戰
- Python語言程序設計
- PostgreSQL Replication(Second Edition)
- AppInventor實踐教程:Android智能應用開發前傳
- 從零開始學Linux編程
- Scala Reactive Programming
- C#開發案例精粹
- Rust游戲開發實戰
- Spring 5 Design Patterns
- IBM Cognos TM1 Developer's Certification guide
- C編程技巧:117個問題解決方案示例
- 算法設計與分析:基于C++編程語言的描述
- OpenCV Android開發實戰