Kodie Grantham
Kodie Grantham

Reputation: 2031

Creating Meteor package library?

I'm wanting to create a "core" package and "module/plugin" packages much like accounts-facebook uses accounts-base in Meteor.

Here's what I have currently but it's not working:

packages/project:modules-core/package.js:

Package.describe({
  name: 'project:modules-core',
    summary: 'Core package for Modules.',
    version: '1.0.0'
});

Package.onUse(function (api) {
    api.versionsFrom('[email protected]');

    api.addFiles('lib/core.js', ['client', 'server']);

    if (api.export) {
        api.export('HBModule');
    }
});

packages/project:modules-core/lib/core.js:

HBModule = (function () {
  var moduleName = "";

  var getShareCount = (function (url) {

  });

  var register = (function (name) {
    HBModule[name] = name;
  });
}());

packages/project:facebook/package.js:

Package.describe({
  name: 'project:facebook',
    summary: 'Facebook Module.',
    version: '1.0.0'
});

Package.onUse(function(api) {
    api.versionsFrom('[email protected]');

    api.use('project:modules-core', ['client', 'server']);

    api.imply('project:modules-core', ['client', 'server']);

    api.addFiles('lib/facebook.js', ['client', 'server']);
});

packages/project:facebook/lib/facebook.js:

Facebook = (function () {
  var moduleName = "Facebook";

  var getShareCount = (function (url) {
     return 22;
  });
}());

HBModule.register('facebook');

And with this I'm getting a TypeError: Cannot read property 'register' of undefined error.

What am I doing wrong?

Thanks!

Upvotes: 0

Views: 220

Answers (1)

Ashley Reid
Ashley Reid

Reputation: 488

The IIFEs (Immediately-Invoked Function Expressions) aren't returning anything, so HBModule and Facebook are both undefined. When creating the HBModule, you need to return an object with a register property set to your register function.

HBModule = (function () {
  var moduleName = "";

  var getShareCount = (function (url) {

  });

  var register = (function (name) {
    HBModule[name] = name;
  });

  // return an object that will be assigned to HBModule
  return { register: register };
}());

Also the IIFE's are unnecessary here, as Meteor will wrap each file anyway, and only expose the variables you use without declaring (such as HBModule); all the variables declared with var will be scoped to that file.

packages/project:modules-core/lib/core.js:

HBModule = {};
var moduleName = '';

// assuming you want to expose the getShareCount method as well?
HBModule.getShareCount = function (url) {

};

HBModule.register = function (name) {
  HBModule[name] = name;
};

packages/project:facebook/lib/facebook.js:

Facebook = {};
var moduleName = 'Facebook';

Facebook.getShareCount = function (url) {
  return 22;
};

HBModule.register('facebook');

Upvotes: 1

Related Questions