Setting up event handler in another function doesn't work

Hi all,

I’m trying to set up an event handler in a package function which looks like:

MyPackage = {
    LocalHistory: function (templateId, formId, form) {

        if (Meteor.isClient) {
            var gHistory = new Mongo.Collection(null);

            this.formId = formId;
            this.templateId = templateId;
            this.form = form;
            this.isDirty = false;

            var schema = AutoForm.getFormSchema(this.formId, this.form);
            gHistory.attachSchema(schema);

            var self = this;
            console.log(this.templateId);


            ReactiveTemplates.events(this.formId, {
                'change form': function () {
                    console.log('form changed');
                    self.markDirty();
                }
            });
           ..........

But the ‘change form’ event handler is never called. I’ve added reactive-template and autoform to the package.js file, but doesn’t help.

api.use('aldeed:autoform');
api.use('nicolaslopezj:reactive-templates');

if I move the handler set up code to some “top-level” scope, it works fine.
Can someone please explain why and how can I make this work in a package?

Can we see your package.js file?

yes

Package.describe({
    name: 'zkuang:history',
    version: '0.0.1',
    // Brief, one-line summary of the package.
    summary: '',
    // URL to the Git repository containing the source code for this package.
    git: '',
    // By default, Meteor will default to using README.md for documentation.
    // To avoid submitting documentation, set this field to null.
    documentation: 'README.md'
});

Package.onUse(function (api) {
    api.versionsFrom('1.2.0.2');
    api.use('ecmascript');
    api.use('aldeed:autoform');
    api.use('nicolaslopezj:reactive-templates');
    api.addFiles('history.js', 'client');
    api.export('History', 'client');
});

Package.onTest(function (api) {
    api.use('ecmascript');
    api.use('tinytest');
    api.use('zkuang:history');
    api.addFiles('history-tests.js');
});

I did a little search in meteor source. The handler is set in onRender hook. Blaze seems to register event handlers when it renders views, which is before calling the onRender hook. So the event handler set in onRender hook is not registered. I have to use jquery event instead.