1

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?

2 Answers 2

4

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...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sure enough, that works! Strange, b/c I am sure I tried that weeks ago. But I must have had another problem that caused it to fail. Thanks, Peppe!
@MichaelMcC Perhaps you had var myFunction = function(){ }? That's kind of the same as function myFunction(){ }.
0

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

2 Comments

I think that the files in the lib folder are available to both client and server, but the functions don't appear to be accessible to functions in other files ... UNLESS you define them like Peppe does above, in which case they work whether or not they are in lib. Somebody clarify this if I'm wrong or over-simplifying.
yes, you have to declare functions as global variables (without var keyword)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.