Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 922
ceth Avatar asked Apr 23 '13 10:04

ceth


People also ask

Does the order of JavaScript files matter?

If I'm understanding your question I think you're asking if it matters where in a file a function/method is defined, and the answer is no, you can define them anywhere in a single source file. The JavaScript parser will read in all symbols before trying to run the code.

Where should I put my js files?

To place JavaScript in an HTML file, use the <script>… </script> tag. You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags.

How does meteor JS work?

Meteor uses data on the wire, meaning the server sends data, not HTML, and the client renders it. Meteor embraces the ecosystem, bringing the best parts of the extremely active JavaScript community to you in a careful and considered way.

Should I put all my JavaScript in one file?

You should put your JS code in a separate file because this makes it easier to test and develop. The question of how you serve the code is a different matter.


2 Answers

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
like image 97
Tarang Avatar answered Oct 16 '22 01:10

Tarang


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.

like image 40
pipedreambomb Avatar answered Oct 16 '22 01:10

pipedreambomb