baseprime / grapnel

The smallest JavaScript router with named parameters, HTML5 pushState, and middleware support
http://grapnel.js.org
467 stars 40 forks source link

Retrigger navigation to current route #69

Closed zlepper closed 7 years ago

zlepper commented 7 years ago

Is there a way to retrigger the current route, so all middleware and actual route will be reloaded?

In most routers you can do this simply by doing router.navigate(currentRoute), however that doesn't seem to trigger navigation again in grapnel, any way this can be done that i'm completely missing?

I have also tried doing router.trigger('navigate'), this didn't help either.

baseprime commented 7 years ago

@zlepper

// Using pushState
router.navigate(router.state.value);

However, if you're using a basic hashchange router, it's less straightforward. In order for Grapnel to remain unobtrusive, it's not going to trigger the window's hashchange event again. What you technically should do is retrigger it. But another option would be to retrigger your router's hashchange event, which will work, but may cause some issues when using multiple routers.

// Using hashchange -- unobtrusive
window.dispatchEvent(new HashChangeEvent('hashchange'));

// Using hashchange -- may cause issues
router.navigate(router.state.value).trigger('hashchange');