Use already existing InertiaController as an API replacement #2199
thoresuenert
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Yesterday, i played around with the new
preserveUrl
feature. I developed the idea to use other inertia routes as endpoints for search selects or dialogs to filter and search data.In one of my projects i have a
form/show route
where I add the relationshipscustomer
anddocuments
to filter and search.To populate the data for my form i have the following Controller:
In another part of the application I also have these
IndexControllers
for both relationships:Now I got the following Idea:
/documents
and/customers
where i already implemented all search functionality (the IndexControllers above)select popover
ordialog
, I dont need to store the data in the url for hard reloadsTo achieve this behavior I need to load the data as follows:
I updated the index endpoint as seen here:
Now instead of loading data from the inertia
show/page
we get the data from thedocuments/index/page
.When we trigger a partial request we load the component from the header, otherwise the default view is rendered[1]
/documents
theX-Inertia-Partial-Component
is set todocuments/index/page
and everything works fineX-Inertia-Partial-Component
is set toshow/page
and we stay on the current page with the current componentWhat do you think about this idea/approach to re use an existing endpoint as an api endpoint for async components like searchable select?
Further work to do:
Find a way to intially load the data. There are multiple options:
reload
useEffect
to trigger the async load when the component is rendering on the page[1] Background:
This approach only works because the inertia router checks if the payload component is the same as the current component and stays on the same page.
Every inertia request has the following header
X-Inertia-Partial-Component
which is the current page component.Beta Was this translation helpful? Give feedback.
All reactions