Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variables in Meteor

I have

var Schemas = {};  Meteor.isClient && Template.registerHelper("Schemas", Schemas);  Schemas.Person = new SimpleSchema({   fullName: {     type: String,     index: 1,     optional: true,   },   email: {     type: String,     optional: true   },   address: {     type: String,     optional: true   },   isActive: {     type: Boolean,   },   age: {     type: Number,     optional: true   } }); 

in one file and

var Collections = {};  Meteor.isClient && Template.registerHelper("Collections", Collections);  Persons = Collections.Persons = new Mongo.Collection("Persons"); Persons.attachSchema(Schemas.Person); 

in another file.

I get the error ReferenceError: Schemas is not defined. It's rather obvious that I have to define Schemas in my collections.js file instead of having them separate. But how does Meteor work with code in separate files? I can access some objects and variables while others are unaccessible.

like image 915
Jamgreen Avatar asked Dec 16 '14 16:12

Jamgreen


People also ask

What are global variables with examples?

Example of Global Variable in C You can notice that in line 4, x and y get declared as two of the global variables of the type int. Here, the variable x will get initialized automatically to 0. Then one can use variables like x and y inside any of the given functions.

Which are global variables?

A global variable is a variable that is declared in the global scope in other words, a variable that is visible from all other scopes. In JavaScript it is a property of the global object.

Where can I find global variables?

Global variables are stored in the data section. Unlike the stack, the data region does not grow or shrink — storage space for globals persists for the entire run of the program. Finally, the heap portion of memory is the part of a program's address space associated with dynamic memory allocation.

What are the two types of global variables?

A global variable can be classified as either session or database based on the scope of the value: The value of a session global variable is uniquely associated with each session that uses this particular global variable. Session global variables are either built-in global variables or user-defined global variables.


1 Answers

When you define a variable in the classic JavaScript way :

var someVar = 'someValue'; 

at the root of your .js file Meteor scopes it to the file using an IIFE.

If you want to define a global variable, simply don't write the var, giving :

someVar = 'someValue'; 

This will define a variable in all your application by default, although you may restrict it by writing that declaration in a specific recognized folder (client or server folder for example).

However this variable won't be defined absolutely first. It will be defined when Meteor runs the actual code that defines it. Thus, it may not be the best practice because you're going to struggle with load order, and it will make your code dependent on how Meteor loads files: which folder you put the file in, the name of the file... Your code is prone to messy errors if you slightly touch your architecture.

As I suggested in another closely related post you should go for a package directly!

like image 124
Kyll Avatar answered Sep 21 '22 23:09

Kyll