Re-rendering blaze templates when a (Flow Router) route changes


#1

This is making me crazy - I can’t seem to get my templates to update when the route changes. They load the first time. Theaction function on the route fires when I click a link to that route. But the BlazeLayout.render call within the action function doesn’t seem to fire again (specifically the helper function on the template isn’t fired and the template doesn’t render again).

I’ve tried adding

Tracker.autorun(function() {
    FlowRouter.watchPathChange();
    BlazeLayout.render( 'applicationLayout', {
      main: 'basicPage',
    });
});

to the bottom of my /lib/routes.js file - but that gives me an error -

TypeError: Object [object Object] has no method 'watchPathChange'
    at lib/routes.js:42:16
    at packages/tracker/tracker.js:99:1
    at Object.Meteor._noYieldsAllowed (packages/meteor/fiber_helpers.js:11:1)
    at packages/tracker/tracker.js:98:1
    at [object Object].Tracker.Computation._compute (packages/tracker/tracker.js:323:1)
    at new Tracker.Computation (packages/tracker/tracker.js:211:1)
    at Object.Tracker.autorun (packages/tracker/tracker.js:562:1)
    at meteorInstall.lib.routes.js (lib/routes.js:40:9)
    at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:141:1)
    at require (packages/modules-runtime/.npm/package/node_modules/install/install.js:75:1)
Exited with code: 8

Can anyone help?


#2

OK - looks like I’ve got the answer. I’ve added

FlowRouter.watchPathChange()

to the relevant helper functions -

Template.sidebarItem.helpers({
  isSelected() {
    FlowRouter.watchPathChange();
    const slug = FlowRouter.current().params.slug;
    if (this.slug === slug) {
      return true;
    } else {
      return false;
    }
  },
});

I don’t have a good enough grasp of reactivity to explain this. But I’m up and running. :smile:


#3

Does the route change or only a parameter? Maybe you’ve the same issue I had a while ago.


#4

Where are you then putting the isSelected() helper in your template?