简体   繁体   中英

In which order 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 my js file to the three ones:

one.js:

function library_f() {
}

two.js:

function some_f() {
  library_f();
}

three.js:

function another_f() {
  library_f();
}

But now I get error

library_f() is not defined

How can I set the order to include my js files ?

From the docs :

  • Files in the lib directory at the root of your application are loaded first. Files that match main.* are loaded after everything else.

  • Files in subdirectories are loaded before files in parent directories, so that files in the deepest subdirectory are loaded first (after lib), and files in the root directory are loaded last (other than main.*).

  • Within a directory, files are loaded in alphabetical order by filename.

These rules stack, so that within lib, for example, files are still loaded in alphabetical order; and if there are multiple files named main.js, the ones in subdirectories are loaded earlier.

But it looks like the functions can't be accessed because they're not global. In meteor each file's variables/functions can't be accessed by another file unless the variable or function is global.

So you would need to declare your function this way:

library_f = library_f() {
...
}

So that it can be accessed by the other files. 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

Meteor scoops up all of your JS files and puts them into one file, and the order is not something you have much control over.

I think the issue here is: where are you calling the functions from? If they're self-executing right away, you're right that the order could be important. However, Meteor usually loads functions into the DOM and then runs them in response to some event like Meteor.startup(callback) or Template.page.rendered(callback) . At the time of execution in these examples, all your JS files will have been loaded and your functions should all be ready to go, in whatever order you like.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM