Jason O.
Jason O.

Reputation: 3290

Sharing global variables between javascript files loaded to Meteor

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

Upvotes: 2

Views: 462

Answers (1)

Jordan Davis
Jordan Davis

Reputation: 1327

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

Upvotes: 1

Related Questions