falsandtru / pjax-api

The advanced PJAX superior to SPA.
https://falsandtru.github.io/pjax-api/
Apache License 2.0
318 stars 28 forks source link

Example on how to setup backend #50

Closed brano543 closed 2 years ago

brano543 commented 2 years ago

Hello,

thank you very much for writing this library. I would like to use your library to implement filter and pagination functionality on my site. There will be three elements that get affected after user clicks:

  1. Filter form
  2. Main area with posts
  3. Pagination links

I am having trouble to understand how I need to setup the backend and point pjax-api to the URL. I am using Wordpress so I need to route to admin-ajax.php endpoint which would handle the request. Moreover how can I tell pjax-api on how to populate the above elements with right content?

Thank you in advance.

falsandtru commented 2 years ago

Pjax requests can be identified by the x-pjax HTTP request header field. Pjax config shouldn't be changed by the server response.

brano543 commented 2 years ago

Thank you for your response. What is still not clear to me is what is mentioned in the documentation here https://falsandtru.github.io/pjax-api/ which states "Multiple area update" support.

Imagine that I have a handler setup correctly and I check the x-pjax header, how would I tell the plugin to correctly update 2 parts in the webpage - ajax would send new filter form and new content body.

falsandtru commented 2 years ago

See the usage section. This config updates the header and the primary element if present.

new Pjax({
  areas: [
    // Try the first query.
    '#header, #primary',
    // Retry with the second query when the first query doesn't match.
    '#container',
    // Retry.
    'body'
  ]
});
brano543 commented 2 years ago

I have checked the usage section multiple times and it is not clear to me. Would you be so kind and answer my questions to give me clarity?

Example: One clickable link Test link Html wil contain two divs, one with #header, second #primary.

Questions:

  1. How do I tell the Pjax on which elements to listen? In this case "test" link.
  2. How should I format the response from /api/test endpoint so that it can render
    Hello
    under #header and
    World
    under #primary.