Destroy or re-create template

helloo,

i have problem with this template below,

Template.myTemplate.onCreated(function(){
  Session.set('docAV', 4);
  var self = this;
  self.showSummary = new ReactiveVar(true);
  self.showA = new ReactiveVar(true);
  self.showCommentForm = new ReactiveVar(false);
  self.showB = new ReactiveVar(false);
  self.editing = new ReactiveVar(false);
  self.sM = new ReactiveVar(false);
  self.message = new ReactiveVar(true);
  self.fT = new ReactiveVar();
  self.M = new ReactiveVar();
  self.MUA = new ReactiveVar();
  self.MUR = new ReactiveVar();
  self.MUV = new ReactiveVar();
  self.autorun(function() {
    if (FlowRouter.getRouteName() === 'A') {
      Meteor.subscribe('SFs', Template.instance().fT.get());
    } else if (FlowRouter.getRouteName() === 'B') {
      Meteor.subscribe('PFs', Template.instance().fT.get());
    } else {
      Meteor.subscribe('AStats', Ps(), Session.get("docAV"), {
        onReady: function () {
          Meteor.subscribe('REsAV', Ps());
        }
      });
    }
    Meteor.subscribe('M', Template.instance().M.get());
    Meteor.subscribe('MU', Template.instance().MUA.get());
    Meteor.subscribe('MU', Template.instance().MUR.get());
    Meteor.subscribe('MU', Template.instance().MUV.get());
  });
});

as You see, i use ReactiveVar and Session extensively to manipulate DOM dan data context on user event,

i just found out that when i change reactive variable from, i don’t know the name so let say default state (i define default state as onCreated’s state) into current state (for example… default state for showSummary is true, and then user change into false on user event), and then i move into another page with the same template myTemplate by clicking other template, say relatedPostit use current state (showSummary is false) , instead of default state (showSummary is true)

this mess up data context because data context should change on user event when variable in default state

is there a way to make all variabel in default state instead of current state on url change…??

my attempts:

  1. using target="_blank" in relatedPost template so this will open related post in new tab, and then page will refresh,then myTemplate will recreated,then all variables will become default state (unfortunately, page refresh is ugly…)
  2. manually change all variable into default state on click event in relatedPost template… as we know ReactiveVar won’t work in cross template, so this require me to convert several ReactiveVar into Session variable… somewhen i’m happy,this work… unfortunately i have list of documents using myTemplate as a document… due to using Session, a change in a document affect all documents…

have another idea…?? help mee… =((

thank Youu,

shouldn’t you be using

self.subscribe(...)

instead of

Meteor.subscribe(...)

also, I believe in onCreate you can use self.fT.get() instead of Template.instance().fT.get()

helloo… @jamgold thanks for your response… i tried but not work as i expected,

any other idea…??

thank You,

ReactiveVar is local. i.e. every new instance gets it’s own store. The desired behaviour you are expecting is correct, whether changing to another route, or even use the same template twice in one page/route, each has it’s own ReactiveVar reinitiated onCreated. Guess you need to check your code for any logic issue.

As a side note, if you do need to use ReactiveVar and Session extensively, I’d recommend to take a look at https://viewmodel.meteor.com . Both logic and code could be much clearer & simpler.

I didn’t check your code. But I did create a few templates which re-created the relations of your use case, for which I could confirm every newly created instance of template does get their own ReactiveVar. I did the test out of my curiosity to confirm ViewModel has the same behaviour. As a result there must be other problems in your code. I’d suggest you to make a simpler version of your code structure to isolate the issue. If you still fail to isolate the issue, put your test repo on github so that to make it easy for others to check and pinpoint the issue for you. There’s no point for others to follow the code of your actual use case.

Check this for other’s comment on how to ask for help:

heloo, @daveeel

i test viewmodel have the same behavior… this might be a behavior from meteor reactivevar,

apologize, i don’t know the right rule for asking question in this forum,

from what i read here…

technical question should be asked in SO… that’s why i post a technical version of this question in SO…

i didn’t repost this question, this question intended for discussion… that’s why i haven’t post a complete code in this forum, and the scope of this question and my so question is very different…

but feel free to delete this question if it against rule,

thank You,

Hi @karina, I’m neither admin nor saying you are against the rules. They are just suggestions from the community. I merely suggesting to simplify/isolate your question so that others could help you better. I’m learning here too :wink:

helloo @daveeel

mmm, i think i won’t go that far,

i just doing a college project…

my supervisor said that no obligation to tweak something to work as expected…the most important things is to try and learn,if something don’t go as expected, then asking…if by asking i get solution, then find out why that solution work… if i don’t get any solution then it safe to assume that it simply a behavior or issue from meteor side…

sooo, thank You for taking Your time =))