Henrique Rotava
Henrique Rotava

Reputation: 801

Meteor, dynamic subscription parameters

I am trying to subscribe to a list of user teams including an extra dynamic team. The id of the last one is passed by parameter to the subscription, like that:

MeteorObservable.subscribe('user.teams', extraTeamId).subscribe(() => {
    this.findUserTeams();
});

The extraTeamId var is constantly changing on my service component, and I need to have this dynamic team on my client collection every time this var changes.

My first question is: if I change the value of this variable on my component, should the publish receive this new value, or it is never updated on server?

If Meteor wasn't supposed to work like that, there is any other good options?

I am also using Angular2 for that.

Upvotes: 1

Views: 574

Answers (1)

Amit kumar
Amit kumar

Reputation: 6147

If you want to get new value then you have to unsubscribe from last subscription and again subscribe to new values. you can do it like this in angular 2 meteor.

on server side publish

   if (Meteor.isServer) {
     Meteor.publish('user.teams', function(option : string) {
                console.log(option);   // you will get your dynamic param value in option
                return findUserTeams.find({"team": option});  // apply your query here
        });
     }

and on client side you can do something like this.

     userteamdata: Observable < any[] > ;
     userteamSub: Subscription;

      this.userteamSub = MeteorObservable.subscribe('user.teams', extraTeamId).subscribe(() => {
    this.userteamdata = findUserTeam.find({}).zone();
  });

now when your extraTeamId value change you can detect that change and do something like this

valuechangefunction(){

    if (this.userteamSub) {
            this.userteamSub.unsubscribe();
          }
      this.userteamSub = MeteorObservable.subscribe('user.teams', extraTeamId).subscribe(() => {
            this.userteamdata = findUserTeam.find({}).zone();
          });
}

i hope this will help :)

Upvotes: 1

Related Questions