2

I have a javascript file that I place in the client/lib folder within my Meteor app. As the file grew bigger, I decided to split it into 3 files and define an object 'App' in the global namespace in order for the 3 files to share the data.

Each file starts with

var app = app || {};

(function () {
    'use strict';

    app.object1 = {

This way, file2 and file3 can still use app.object1, and so on.

The problem is when Meteor loads the files, it seems to automatically wraps it with function(){}, and this makes app.object1 not accessible from files loaded subsequently.

(function(){
    var app = app || {};

   (function () {
   'use strict';

    app.object1 = {

What is the best way to avoid this issue? Thanks.

EDIT: I referred to this posting [Link:][1]Global variables in Meteor which suggests defining the variable without "var". I replaced the code in file1 to app = {}, but my app is now crashing in file2 in the following line of code, with the message from the Meteor console pasted below.

app.ALL_LIST = 'all'

=> Your application is crashing. Waiting for file change. ReferenceError: app is not defined

2
  • possible duplicate of Global variables in Meteor Commented Jul 3, 2015 at 17:37
  • Thanks for the helpful link. Followed the instruction in that link and created a package which exports a global variable. Now it works. Commented Jul 4, 2015 at 1:58

1 Answer 1

1

omit var in your variable declaration ;) then it will be scoped globally.

Sign up to request clarification or add additional context in comments.

1 Comment

It's helpful to namespace your globals: medium.com/@sirchill3/…

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.