Sammy loses control of routing when combining back button and history.replaceState

262 views Asked by At

I am having an issue with Sammy routing, and combining use of the back button with history.replaceState. Is there an alternative way to replace the browser's URL without adding to the history stack, and fire a Sammy route other than history.replaceState?

history.replaceState triggers the Sammy route initially, but if I use history.pushState, then click the back button, then history.replaceState, it does not trigger the Sammy route.

It also seems to break if I use location.hash or location.href in combination with history.replaceState

I am wanting to use replaceState to control pagination and sorting of a table, but let the standard history stack work for different search parameters and navigating to different pages and back to a search screen.

Edit: I am looking into ways to set Sammy's internal URL without triggering routing.

1

There are 1 answers

0
MarkosyanArtur On

Faced with the same problem. Solved with:

history.replaceState(path);
sammy.setLocation(path);

First- update browser history (sammy doesn't trigger location changes)

Second- force sammy to raise events with new path.

Also custom route logic is needed:

Sammy.mapRoutes([{'get': 'myPath', callback}]) 

Callback should parse new path, and if only params after '?' symbol are changed, then pass these parameters to the viewModel's method.

Then inside this method you could handle changes like this:

public onParamsChanged({page}) {
    this.myTable.setPage(page);
}