defunkt / jquery-pjax

pushState + ajax = pjax
https://pjax.herokuapp.com
MIT License
16.73k stars 1.97k forks source link

Use pjax without making a server request #381

Open marczking opened 10 years ago

marczking commented 10 years ago

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 with back/forward-buttons pjax:popstate does not fire on the manually set pushstates.

So is there a way I can do this with pjax?

trompx commented 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 ?

mislav commented 10 years ago

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.

kirkbushell commented 9 years ago

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.

samdark commented 9 years ago
    // 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();
samdark commented 9 years ago

But overall I'd love to be able to prevent fragment replacing if pjax:popstate handler returns false or prevents default.