How can I declare a package with shared definitions in multiple files?

Apologies if this is a naive questions. I’m still quite fresh to Javascript and am reading the YDKJS series in parallel to learning Meteor.

I am following the Meteor Guide. As I am looking at the methods section I am trying to clean up some of my code. I try to refactor my methods as per the section Advanced Method Boilerplate. As I do this, I however also want to reduce duplication as I have a SimpleSchema definition with my definitions of collections.

After some refactoring I get the error message ReferenceError: PackageName is not defined at models/order/server/methods.js

I would really like to retain a separate folder structure and definitely don’t want to use a single file with Meteor.isClient/Server calls. How can I make sure the declarations for

PackageName = {};
PackageName.Orders = {};

are visible to all source files?

The structure of my project sis as follows:

      [templates and template.js files]

I have moved the following to the collections.js file:

PackageName = {};
PackageName.Orders = {};
PackageName.Orders.schema = { 
  OrderCollection: new SimpleSchema({ /* Definition */),
  OrderItemsCollection: new SimpleSchema({ /* Definition */)

My method is defined in the methods file that starts with:

PackageName.Orders.methods = {};
PackageName.Orders.methods.setValue ={
     /* Methods boilerplate code */

Note PackageName is just a placeholder.

Put this in a file in the PackageName/lib folder. lib folders get loaded before all other folders.

1 Like

Thanks, I had read that a long time ago and did not find this bit. That worked a treat.

But it would probably be a good idea after 1.3 is out to learn to write modules.

1 Like