I'm learning meteor framework coming from traditional JavaScript background where normally we define a util.js
or application.js
holding collection of useful helper functions which can be used by various JavaScript components and other JavaScript functions defined in various different files. I'm looking for something similar in Meteor. To make it more clear, I'll make a very simple example.
In my Meteor app I want to define a common JavaScript function to check if a string is empty like below in a file utils.js
function isEmpty(val) {
return !$.trim(val);
}
How can I define function in a separate file (I dont want to mix common functions in a template file which is specific to some functionality) such that its available in all other JavaScript files holding template helpers and events? For example in below template.js
01. Template.myform.events({
02. "submit .js-save-data": function(event) {
03. var title = event.target.title.value;
04. console.log('Is title empty: ' + isEmpty(title));
05. }
06. });
07.
08. Template.mylist.helpers({
09. doSomething: function() {
10. console.log('Checking empty: ' + isEmpty('some value');
11. }
12. });
In above example how can I access isEmpty
function from both helper
(line 4) and events
(line 10)?
Update: I can do this old-school-way like defining global scope function in a separate file and then can access it in helpers and js files, but I was looking for something if there is any meteor way doing? Like using Template.registerHelper
.