Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  26] [ 3]  / answers: 1 / hits: 33659  / 10 Years ago, tue, december 16, 2014, 12:00:00

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.


More From » node.js

 Answers
18

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!


[#68470] Friday, December 12, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darennevina

Total Points: 422
Total Questions: 128
Total Answers: 105

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
;