Accessing events and helpers of Package Template

I created a local package and added to my project, and added templating.

package.js

`Package.onUse(function(api) {
  api.versionsFrom('1.4.2.3');
  api.use('templating', 'client');
  api.addFiles('server/main.js', 'server');
  api.addFiles('client/main.js', 'client');
  api.addFiles('client/main.html', 'client');
  api.mainModule('pkgName.js');
});`

I created a template in client/main.html.

<template name="myTemplate">
    <button>Test</button>
</template>

Then in client/main.js, i set the event listener:

Template.myTemplate.events({
  'click button': function (e,t){ //do this on click };
})

But when i run the app - i get console error

Uncaught TypeError: Cannot read property 'events' of undefined

OK - after a couple of hours of research it seems that the Package.onUse function must list dependencies in the order that they should be loaded.

I thought the load order was only relevant to the ‘packages’ file in the main application, but it seems that you need to load the dependencies in order as well.

So this fixed it—

Package.onUse(function(api) {
  api.versionsFrom('1.4.2.3');
  api.addFiles('client/main.html', 'client');  

// I moved the html file above the javascript - so the DOM loads first and then the template exists for the event listener to listen to. 

  api.use('templating', 'client');
  api.addFiles('server/main.js', 'server');
  api.addFiles('client/main.js', 'client');
  api.mainModule('pkgName.js');
});