In what order does Meteor include my js files

I had one js file:

function library_f() { } function some_f() { library_f(); } function another_f() { library_f(); } 

But the code looks ugly, and I decided to split the js file into three:

one.js:

 function library_f() { } 

two.js:

 function some_f() { library_f(); } 

Three.js:

 function another_f() { library_f(); } 

But now I get the error

 library_f() is not defined 

How can I adjust the order of inclusion of my js files?

+4
source share
2 answers

From docs :

  • First, the files in the lib directory in the root folder of the application are loaded. Files that match main. * Download after everything else.

  • Files in subdirectories are loaded before files in parent directories, therefore, files in the deepest subdirectory (after lib) are loaded first, and files in the root directory are downloaded last (except for the main. *).

  • In the directory, files are downloaded in alphabetical order by file name.

These are stack rules, so in lib, for example, files are still loaded in alphabetical order; and if there are several files named main.js, then they are loaded earlier in subdirectories.

But it seems that the functions are not available, because they are not global. In meteor, each file / variable / function cannot be accessed by another file, unless the variable or function is global.

So you need to declare your function as follows:

 library_f = library_f() { ... } 

So that other files can access it. The same goes for variables:

 var x = true; //Not accessible by other files x = true; //Accessible by other files var dothis = function () {...} //Not accessible by other files dothis = function() {..} //Not accessible by other files function dothis() {..} //Not accessible ny other files 
+19
source

Meteor takes all your JS files and puts them in a single file, and the order is not something that you have a lot of control.

I think the problem is here: where are you calling the functions from? If they are self-made right away, you are right that order can be important. However, Meteor typically loads functions into the DOM and then runs them in response to some kind of event, such as Meteor.startup(callback) or Template.page.rendered(callback) . At run time in these examples, all of your JS files will be loaded, and your functions should be ready to work in any order.

If you are not trying to start them as soon as they are created, but later, it is worth checking in the browser console that you can see that all functions are present in the DOM after the page loads. Enter library_f and see what comes back.

+2
source

All Articles