Meteor package only approach, calling namespace function from a different package

166 views Asked by At

I am using the structure design shown at Meteor Meetup Antwerp by Pieter Soudan

I have had success by having different namespace names (UserAuth,AppRoute) depending on the functionality of my module. I however want to have an app specific namespace UP (UserPortal) and have namespaces like UP.UserAuth, UP.AppRoutes.

I can't seem to call a function in UP.UserAuth that checks for login.

My up-app package package.js looks like this

Package.describe({
  name: 'up-app',
  version: '0.0.1',      
  summary: 'User Portal Application',

});

var both=['server','client'];
var server ='server';
var client ='client';

Package.onUse(function(api) {
  api.versionsFrom('1.0.3.2');
  api.use('templating',client);
  api.use('iron:[email protected]',both);
  api.use('tracker',both);
  api.use('underscore',both);
  api.use('blaze',both);

 api.use(['up-user-auth'],both);



  api.addFiles(['lib/namespace.js','lib/routes.js'],both);

  api.addFiles(['views/dashboard.html','views/loading.html'],client);   


  api.export('UP');

});

Package.onTest(function(api) {
  api.use('tinytest');
  api.use('up-app');
  api.addFiles('tests/up-app-tests.js');
});

I intend to use up-app to declare all my app dependencies within a single package.

My up-app/lib/routes.js file looks like this:

Router.configure({
    layoutTemplate: 'upDashBoard',
    loadingTemplate: 'upLoading'
});

Router.onBeforeAction(UP.UserAuth.loginRequired, {
    except: ['login','install']

});

Router.route('/', {
    name: 'home'
});

My up-user-auth package has this in its package.js

Package.describe({
  name: 'up-user-auth',
  version: '0.0.1',
  // Brief, one-line summary of the package.
  summary: 'User Authentication and Roles Management',

});

Package.onUse(function(api) {
  var both = ['server', 'client'];
  var server = 'server';
  var client = 'client';


  api.versionsFrom('1.0.3.2');

  api.use([
        'tracker',
        'service-configuration',
        'accounts-base',
        'underscore',
        'templating',
        'blaze',
        'session',
        'sha',
      ]
      ,client);

  api.use([
        'tracker',
        'service-configuration',
        'accounts-password',
        'accounts-base',
        'underscore',]
      ,server);

  api.use(['iron:[email protected]','copleykj:mesosphere'], both);
  api.imply(['accounts-base','accounts-password'], both);

  api.addFiles(['lib/namespace.js','lib/loginValidation.js','lib/loginMethods.js'],both);


    api.export('UP', both);

});

Package.onTest(function(api) {
  api.use('tinytest');
  api.use('up-user-auth');
  api.addFiles('tests/server/up-user-auth-tests.js');
});

My up/lib/namespace.js looks like this:

UP={};
UP.UserAuth={
    loginRequired: function() {
        return console.log("loginControllers");
    }


}

If I remove the second reference to UP={}; then I get an error saying Cannot set property 'UserAuth' of undefined but when I add it all I get is Cannot read property 'loginRequired' of undefined

What am I doing wrong?

1

There are 1 answers

1
Sewdn On

your forgot to declare the dependency on your main package 'up-app'. Every package using the namespace UP, should declare dependency on the package exporting the namespace.

So, just add

api.use('up-app', ['client', 'server']);

in up-user-auth/package.js

Kr, Pieter