javascripthtmlmeteormeteor-blazeflow-router

Meteor links based on reactive var not working


I try to create some links which adapt to the current url, so when my current url is "object/layout1....", my link should also be like that and when the url is like "object/layout2", it should be "object/layout2.." for the link. Therefore, I create this code which should save the current layout in a reactive variable and should refresh due to the autorun when the url changes. This part works fine:

Template.templateName.onCreated(function(){
  //Save parameter layout in reactive var so it can be accessed by helper
  this.getParam = () => FlowRouter.getParam('parameter');

  this.autorun(() => {
    this.layout = new ReactiveVar(this.getParam());
  });
});

The problem is that the helper return the new link is not updating. When loading the page, there is the right link, but after changing the url nothing changes for the link. Here the code for the helper:

'returnLink':function(postId) {
    return '/object/' + Template.instance().layout.get() + '?firstList=' + postId;
  }

What am I missing, why is the link not updating in my layout? I'm using the helper in my layout simply like <a href="{{returnLink this._id}}">My new Link</a>

And yes, I'm using flowrouter


Solution

  • First of all, if you want the URL to be /object/layout1/... when you are on /object/layout1, then you van use a relative URL and get rid of all the onCreated() code, like so:

    'returnLink':function(postId) {
        return '?firstList=' + postId;
    }
    

    If you still need reactive in flowrouter, the docs says FlowRouter.getParam() is reactive, so, once again you can get rid of all the onCreated() code and just use this:

    'returnLink': function (postId) {
        return '/object/' + FlowRouter.getParam('parameter') + '?firstList=' + postId;
    }
    

    Give it a try and let me know how it went.