Search code examples
meteorreloadflow-router

How to use Meteor FlowRouter.reload()


I've found the FlowRouter documentation for FlowRouter.reload(), but I've been unable to find specific code examples and can't get it working.

In my app, I have a template that uses some clever javascript (Isotope) to reposition elements as the page resizes. Sometimes the user navigates away, resizes the browser window, and then returns - to a messed up page that should refresh and redraw to reposition the elements for the re-sized window.

This is my route. How would I use FlowRouter.reload() to reload/refresh just the "work" template area? Alternatively, how would I use it to reload/refresh the whole layout template or window?

FlowRouter.route( '/work', {
  action: function() {
    BlazeLayout.render( 'body-static', { 
      content:  'work',
    });
  },
});

Solution

  • In case anyone else comes here, this was solved with a great help from Hugh in the Meteor forum.

    The solution did not use reload. Instead, it made use of Triggers in FlowRouter to check if the template was being reloaded, refresh it if so, and then stop once refreshed to prevent an endless loop.

    Here is how it worked out.

    // handle refreshing div on every load of div
    let fireReload = false;
    
    function reloadCheck(context, redirect, stop) {
      if (fireReload) {
        console.log('Hugh is Awesome and also reloading screen...');
        FlowRouter.reload();
        stop();
      }
    }
    
    function routeCleanup() {
      fireReload = !fireReload;
    }
    
    FlowRouter.route('/work', {
      action: function() {
            BlazeLayout.render( 'body-static', { 
                content:    'work',
            });
      }, 
      triggersEnter: [reloadCheck],
      triggersExit: [routeCleanup]
    });