Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event "pjax:beforeApply" to update options #550

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

SamyPesse
Copy link
Contributor

This PR is a simple proposal to add a pjax:beforeApply event, which let us change the container (and some others options) according to the content returned by the AJAX request.

I'm using this to load content into different containers: main page and modals. A modal is just some content displayed on top of the main page without changing the page's url. So we have a static website that looks dynamic by remotely loading modals.

I understand that it's probably not an intended usage of pjax.

Example:

<body>
    <div id="pjax-container"></div>
    <div id="modals-container"></div>
</body>
$(document).pjax('a', '#pjax-container')
.on('pjax:beforeApply', function(e, xhr, content, updateOptions) {
    var isModal = xhr.getResponseHeader('X-PJAX-IsModal');

    if (isModal) {
        updateOptions({
            container: "#modals-container",
            push: false
        });
    }
});

Current solution: bind with different options

The current solution is to apply pjax according to an attribute:

$(document).pjax('a[data-pjax!=modal]', '#pjax-container');
$(document).pjax('a[data-pjax=modal]', '#pjax-modal', {
    push: false
});

The issue with this solution is to ensure that all links have the correct target (data-pjax). Also it doesn't work for submitting a form in a modal (data-pjax=modal) that causes a redirection for the main page (the redirected content will be displayed in the modal container).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant