[SOLVED] Template subscriptions

Hello,

I ran across something I don’t understand. Consider the following code

<template name="dashboard">
    {{> userAds}}
    {{> map}}
</template>

Template.userAds.onCreated(function () {
    this.subscribe("savedAds");
});

Template.map.onCreated(function() {
  this.subscribe("adLocalisation"); });

Meteor.publish("savedAds", function(){
  return Ads.find({createdBy : this.userId}, {
    fields: {
      title:true
    },
    sort : {
      createdAt : -1}
    });
});

Meteor.publish("adLocalisation", function(argument){
  return AdLocalisation.find({createdBy: this.userId});
});

From what I understand, data from Ads collection should not be available in the map template.

However, this
console.log(Ads.find({}, { fields: {title:true} }).fetch());
returns the data in Ads collection.

Then, if I remove the subscription from the userAds template, the find does not returns any result…
Did I miss something here ?

Thanks !

Just to understand better, can you confirm you have removed autopublish and insecure?

Tat

Yes I have. I think that as soon a subscription is made, then the data is available on the client side regardless of the template

1 Like

Not sure this will help but:

I always have trouble using this anywhere, as the scope of it confuses me in Blaze. In React, which i’m trying to pick up at the moment, it is quite a bit more specific.

Can you try code like below:

Template.usersAds.onCreated( () => {
Template.instance().subscribe(‘savedAds’);
});

That way it may be possible to eliminate if ‘this’ is doing some odd scoping thing. Just a thought. Unfortunately i am quite new to both Meteor and programming.

Tat

Template level subscriptions don’t mean that particular subscription is available only for the template in which it was called and its children.

So your code works as it should be.

2 Likes

@brajt Does that mean that the subscription is available to all the templates ?

@tathagatbanerjee Tried with instance and got the same results !

Yes, the subscription is available to all templates even if it’s made in particular template.

1 Like