Michael McC
Michael McC

Reputation: 689

Meteor and javascript: make functions in one file available to another

I use iron-router with Meteor and I have written a number of functions that I call from the Router.map() which defines all my routes and hooks. The file is getting to be cumbersome to scroll around in and I would like to move my functions to a different file.

The only way I've found to make functions in one file available to those in another file is to define those functions in a script tag inside the head tag. But of course, I'd rather not put them there. I assume there's a straightforward way to do this?

Upvotes: 1

Views: 990

Answers (2)

Peppe L-G
Peppe L-G

Reputation: 8345

You create the functions the following way?

function myFunction(){
    // Your code...
}

This creates a local variable storing your function (all code in each js-file is wrapped in a function!). You must instead store your function in a global variable, which can be done in the following way:

myFunction = function(){
    // Your code...
}

Upvotes: 4

Tomas Hromnik
Tomas Hromnik

Reputation: 2200

Create a folder named "lib" in your project. Then create functions.js in the lib directory and put there all your functions.

See http://docs.meteor.com/#structuringyourapp

Upvotes: 0

Related Questions