Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call functions from separate files with Meteor

I want to create an application in Meteor, from what I understand the manual it first loads which are in subdirectories, and then follows the alphabetical order. My file structure is like this ...

/server /lib /client     /lib         game.js -> already starts declaring the function "makeBoard(){}"     /template.js -> where the function "makeBoard()" is called. 

thus appears that the error function "makeBoard()" does not exist, only works if I declare in the same file where I want to call it. Even when I move the file "game.js" to the same directory where "template.js", it happens. How should I correctly make references to resources that are in different files on Meteor?

like image 814
rogeriojlle Avatar asked May 14 '13 02:05

rogeriojlle


2 Answers

Using a globally defined variable, as avital suggests, will work, but is not a recommended code design choice (see JS mistake 1 listed here).

Instead in your lib directory you could create a file with:

Meteor.myFunctions = { ...     makeBoard : function() { ... }, ... } 

Then in any other js file you could call Meteor.myFunctions.makeBoard(). This should be done in the lib directory because Meteor guarantees the js files in lib are loaded before other directories, so your function will already be loaded.

like image 87
Shwaydogg Avatar answered Sep 23 '22 05:09

Shwaydogg


Define the function with makeBoard = function() { ... }.

Functions defined with function foo() { ... } are local to the file, as are variables defined with var bar = ....

like image 33
avital Avatar answered Sep 22 '22 05:09

avital