Open marczking opened 10 years ago
+1 I am trying to do the same (I just need to fade an overlay to go back to the main main page and change url without breaking back/forward). Reloading the same main content just to change the url is overkill.
I already tried to simulate a pjax cachepush followed by a pushstate with a manually created state but pjax:popstate is not firing.
So is there a way to add a state that would trigger pjax:popstate manually ?
@marczking did you come up with a solution ?
Right now it's tricky to mix pjax stuff with manual pushState/popstate stuff. You shouldn't expect pjax:popstate
to fire, though, if you didn't use pjax. If it does, that's a bug.
I'll leave this open as a reminder that we need to improve pjax in scenarios where you mix-n-match it with other pushState functionality.
This kind of requirement usually smells of an anti-pattern. You generally only want pushstate updates when big changes happen (such as general content replacement). Anything smaller is usually a code smell. You don't want URLs acting as application state management tools.
// disable and then re-enable to make sure our handler is before PJAX's
$.pjax.disable();
$(window).on('popstate', function (e) {
if (!needPjax()) {
$.pjax.disable();
e.stopImmediatePropagation();
// do your non-PJAX stuff here
$.pjax.enable();
}
});
$.pjax.enable();
But overall I'd love to be able to prevent fragment replacing if pjax:popstate
handler returns false
or prevents default.
Sometimes I want to only change some small things with JS only for which I don't need to make a request to the server. I would still like to change the
history.pushState
though.When using
history.pushState
manually I get my wished behaviour, only problem is when navigating withback/forward
-buttonspjax:popstate
does not fire on the manually setpushstates
.So is there a way I can do this with pjax?