How do I reload the current iron:router route?
Asked Answered
C

2

7

If I'm currently at /foo, Router.go '/foo' does nothing. I would like /foo's action hooks and rendering to be redone. I know I could make a dependency, mention it in an action hook, and invalidate it when I need to reload, I'm just hoping there's a Router.* api I can use, because that would be cleaner.

Coquette answered 5/2, 2015 at 7:8 Comment(7)
Drop the / - > Router.go('foo')Octad
That's not working for me either. At least not calling onBeforeAction again.Coquette
Why are you trying to re-render the current route?Droit
you can try window.location.reload() this.Overstretch
@ajduke: I'd rather not make a new http request / reload the whole page (takes much more time). I have a global non-reactive (needs to be) login-check onBeforeAction that renders the login template instead of the current route's template. Once the user logs in, I want that function to rerun and load that route's template (but only when I tell it to, not reactively based on Meteor.userId()).Coquette
imho there needs to be a native solution for this - the "reload current view" scenario is one of the most basic aka "cancel"Disjoint
I think iron-router needs to add an option for routes to allow for the same route to re-execute. I have the same problem where we want to run the same search term again and we cannot have the page reactive because the data comes from an external source. I have to force the page change with a bogus incrementing query parameter.Leff
C
3

This adds a function Router.rerun() that works:

login_dep = new Tracker.Dependency

Router.rerun = ->
  login_dep.changed()

Router.configure
  onBeforeAction: ->
    login_dep.depend()
    ...
Coquette answered 6/2, 2015 at 5:44 Comment(0)
P
0

There is a way with iron router:

Router.current().render(Template.yourMainTemplateName).data();

I wouldn't recommend it though. Isn't there a way to rewrite it so it doesn't need to reload?

Another solution (perhaps better, depends on the use case) would be to have an Autorun function in your main template rendered callback. If you define your dependencies with Template.getData() it should run the code inside whenever the data changes.

Panto answered 5/2, 2015 at 12:0 Comment(1)
That doesn't rerun eg the onBeforeAction hook. And doesn't work on layout templates.Coquette

© 2022 - 2024 — McMap. All rights reserved.