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

Community
  • 1
  • 1
Jason O.
  • 3,168
  • 6
  • 33
  • 72
  • possible duplicate of [Global variables in Meteor](http://stackoverflow.com/questions/27509125/global-variables-in-meteor) – Jordan Davis Jul 03 '15 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. – Jason O. Jul 04 '15 at 01:58

1 Answers1

1

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

Jordan Davis
  • 1,271
  • 14
  • 24
  • 1
    It's helpful to namespace your globals: https://medium.com/@sirchill3/meteor-managing-the-global-namespace-5a50080a05ea – rpggio Jul 05 '15 at 07:50