Template subscriptions and autorun

Not sure I understand quite what you’re asking. Helpers are defined in a Template.xxx.helpers() block - so not within the context of a Template.xxx.onRendered()

Hi,

I really wonder… I just use the code from this thread, but my subscription to apps is not being re-run. I would like to show only the apps with an ID that is present in the GeneratedResources collection…

Template.generation.onCreated(function() {
    var self = this;
    self.autorun(function(){       
        self.subscribe('generatedResources', function() {
            console.log('generatedResources changed, so update the apps subscription');
            const generatedAppsFromUser = GeneratedResources.find()
            .map(function(resource) {
                return resource.appId;
            });
            console.log('onCreated generatedResources are: ', generatedAppsFromUser);
            //now get all the apps from Qlik Sense, but filter them so that only the apps are show which the current user has generated
            Meteor.subscribe('apps', generatedAppsFromUser);            
        });
    })
})

My publication

Meteor.publish('apps', function(generatedAppsFromUser) {
    if (Roles.userIsInRole(this.userId, ['admin'], Roles.GLOBAL_GROUP)) {
        return Apps.find();

    } else {
        console.log('Client subscribed to collection, with these generated app ids: ', generatedAppsFromUser);
        if (!generatedAppsFromUser) {
            generatedAppsFromUser = [];
            console.log('##### No generated resources exists yet, so only show the template apps')
        } else{
            console.log('### publication recevied these generated app ids for the user: ', generatedAppsFromUser);
        }
        return Apps.find({
            $or: [{ "id": { "$in": generatedAppsFromUser } }, { "stream.name": "Templates" }, { "stream.name": "Everyone" }]
        });
    }
    this.ready();
});

Funny, if I use another tracker.autorun inside it works… But I am a bit lost now :slight_smile:

  • why is this happening?
  • what is the difference between tracker.autorun and this.autorun?
  • is the issue in my previous code, that I have put the subscription to apps in the callback function on the GeneratedResources collection on which it has a dependency?

thank you

Template.generation.onCreated(function() {
    this.subscribe('streams');
    this.subscribe('customers');

    var self = this;
    self.autorun(function() {
        self.subscribe('generatedResources', function() {
            console.log('generatedResources changed, so update the apps subscription');
            Tracker.autorun(function() {
                const generatedAppsFromUser = GeneratedResources.find()
                    .map(function(resource) {
                        return resource.appId;
                    });
                console.log('onCreated generatedResources are: ', generatedAppsFromUser);
                //now get all the apps from Qlik Sense, but filter them so that only the apps are show which the current user has generated
                Meteor.subscribe('apps', generatedAppsFromUser);
            })

        });
    })
})

Source: http://stackoverflow.com/questions/29443513/meteor-tracker-autorun-observechanges-collections-not-working-as-expected