Accessing template level subscription inside event handler - syntax?

I am having trouble accessing a subscription inside an event handler:

on the SERVER, I am publishing:

Meteor.publish('basicInfoPub',function() {
   
    if (!this.userId) {
        return this.ready();
    }
    return BasicInfo.find(
        {  userId:this.userId },
        { fields: {firstName:1, lastName:1, middleName:1, someCodes:1}
    });

});

On the CLIENT, am subscribing, and then trying to access the subscription data in an event handler.

Template.basicInfo.onCreated(function(){

    basicInfoSub = this.subscribe('basicInfoPub');

});
//event handler
    'click #populateFormButton': function(e,instance){
        if(instance){
            $('#firstName').val(instance.data.basicInfoSub.firstName); 
            $('#lastName').val(instance.data.lastName);
            $('#middleName').val(instance.basicInfoSub.middleName);
        }
    }

In the event handler, none of the 3 options shown actually work (instance.data, instance.basicInfoSub, or instance.data.basicInfoSub). How do I access firstName, lastName etc inside the event handler?

Thanks!

You donā€™t need to access the template instance. The data that is returned by the subscription goes into the (globally accessible) client-side cache, minimongo. All you need to do to get the data on the client is:

'click #populateFormButton': function(e, instance){
  var basicInfo = BasicInfo.findOne({_id: Meteor.userId()});
  instance.$('#firstName').val(basicInfo.firstName); 
  instance.$('#lastName').val(basicInfo.lastName);
  instance.$('#middleName').val(basicInfo.middleName);
 }

This is assuming the data has been returned from the subscription by the time the user clicks the button (pretty safe bet).

Also note the instance.$ rather than $, so the scope in which jquery searches is limited to html in that particular template instance.

(The BasicInfo collection should be instantiated on both client and server.)

1 Like

Thanks for the clear explanation!