Meteor, where to define my collection?

I have a pretty simple application structure containing those libs

server - contain some configuration for routing and ENV

client - contain templates ( <template name=".*"></template> ) and a JS file for each template

collections

now inside the collections I have a file called "Albums.js" and has pretty simple content

var Albums = new Meteor.Collection("Albums");

now inside my client folder I am trying to access this Albums variable and I get an undefined error.

my goal is to take data from form and pass it to collection.insert

+7
meteor
source share
3 answers

Do not use var unless you want it to be private to this file.

Albums = new Meteor.Collection ("Albums");

+12
source share

another way to define global variables is to create a new file, for example. collections.js and place it directly in the root folder of your application (not in any subfolder!)

In this file you can define a global variable / collection (without the var keyword)

+5
source share

Variables defined with the var keyword are local to the file in which they are defined. If you want a global variable shared by files, you need to define it without the var keyword.

It seems that this is not in the document, but in the file https://github.com/meteor/meteor/blob/master/History.md (for version 0.6.0):

Variables declared with var at the outermost level of the source JavaScript file are now private to this file. Remove the var variable to share the value between the files.

Basically, each JS file is wrapped in a template (function(){ ... })(); to provide this encapsulation.

+4
source share

All Articles