Reputation: 2200
I have a pretty simple app structure that contain those libs
server
- contain some configuration for routing and ENV
client
- contain templates (<template name=".*"></template>
) & JS File for every template
collections
now inside collections i have file called "Albums.js" and has a pretty simple content
var Albums = new Meteor.Collection("Albums");
now inside my client folder i'm trying to access to this variable Albums
and i'm get undefined error.
my target is to take data from form
and pass it to collection.insert
Upvotes: 7
Views: 2164
Reputation: 5990
another way to define global variables is to create new file, e.g. collections.js
and put it directly into Your app root folder (not into any subfolder!)
In this file You can define global variable/collection (without var
keyword)
Upvotes: 5
Reputation: 3682
Do not use var unless you want it private to that file.
Albums = new Meteor.Collection("Albums");
Upvotes: 12
Reputation: 19544
Variables defined with var
keyword are local to the file they're defined in. If you want a global variable, shared across files, you need to define it without the var
keyword.
It looks like it's not in the doc, but it's in the https://github.com/meteor/meteor/blob/master/History.md file (for version 0.6.0):
Variables declared with var at the outermost level of a JavaScript source file are now private to that file. Remove the var to share a value between files.
Basically, each JS file is wrapped in the (function(){ ... })();
pattern to provide this encapsulation.
Upvotes: 4