0

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.

Jay
  • 1,539
  • 1
  • 16
  • 27
  • 1
    This isn't a duplicate. The moderobots didn't read the questions. For me, I create an object with helper functions for all my templates, then `Template.mylist.helpers(allHelpers)`. I'd post an example, but I'm not allowed to help you that much :-/ – Michael Cole Dec 02 '15 at 15:53
  • @MichaelCole thanks for your comment. See if you can post some example. It will be good help to learn Meteor. Else for now I've just created a good old global function in my `utils.js` which I can then access it template helpers and events. – Jay Dec 02 '15 at 22:21

0 Answers0