Replies: 3 comments 13 replies
-
@alexandre-castelain based on your example in #180 (comment), how would we handle the |
Beta Was this translation helpful? Give feedback.
-
Correct me if I'm wrong, but I see a small issue with all that logic. Let's assume that I have following controller: class UserController extends AbstractController
{
use DataTableControllerTrait;
public function show(Request $request, User $user)
{
$userPosts = $this->createDataTable(PostDataTableType::class, ...);
$userComments = $this->createDataTable(CommentDataTableType::class, ...);
$userPosts->handleRequest($request);
$userComments->handleRequest($request);
return $this->render('user/show.html.twig', [
'user_posts' => $userPosts->createView(),
'user_comments' => $userComments->createView(),
]);
}
} Now, let's say we modify request handler to execute only if class HttpFoundationRequestHandler implements RequestHandlerInterface
{
public function handle(DataTableInterface $dataTable, mixed $request = null): void
{
$turboFrame = $request->headers->get('Turbo-Frame');
if (null !== $turboFrame && $turboFrame !== $dataTable->getConfig()->getTurboFrameIdentifier()) {
return;
}
// Current handle() logic
}
} If user interacts with if ($userPosts->isRequestFromTurboFrame()) {
return $this->renderDataTableTurboStream($userPosts);
}
if ($userComments->isRequestFromTurboFrame()) {
return $this->renderDataTableTurboStream($userComments);
} I don't see a way to make it compatible. However, my last suggestion was to leave the request handler as-is, and call class UserController extends AbstractController
{
use DataTableControllerTrait;
public function show(Request $request, User $user)
{
$userPosts = $this->createDataTable(PostDataTableType::class, ...);
$userComments = $this->createDataTable(CommentDataTableType::class, ...);
if ($userPosts->isRequestFromTurboFrame()) {
$userPosts->handleRequest($request);
return $this->renderDataTableTurboStream($userPosts);
}
if ($userComments->isRequestFromTurboFrame()) {
$userComments->handleRequest($request);
return $this->renderDataTableTurboStream($userComments);
}
$userPosts->handleRequest($request);
$userComments->handleRequest($request);
return $this->render('user/show.html.twig', [
'user_posts' => $userPosts->createView(),
'user_comments' => $userComments->createView(),
]);
}
} Maybe we could pass the request to the |
Beta Was this translation helpful? Give feedback.
-
Hi @Kreyu ! Hi @FluffyDiscord ! I tried to implement what we discussed last week. You can find it here : https://github.com/alexandre-castelain/data-table-bundle/tree/async-and-turbo Several information :
//UserDataTable.php
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'is_asynchronous' => true,
]);
}
//MyController.php
public function index(
ProductRepository $productRepository, Request $request,
): Response
{
$query = $productRepository->createQueryBuilder('product');
$dataTableProduct = $this->createDataTable(ProductDataTableType::class, $query);
$dataTableProduct->handleRequest($request);
if ($dataTableProduct->isRequestFromTurboFrame()) {
return $this->renderDataTableTurboFrameResponse($dataTableProduct);
}
return $this->render('home/index.html.twig', [
'products' => $dataTableProduct->createView(),
]);
} Other consideration :
What do you think about it ? |
Beta Was this translation helpful? Give feedback.
-
Based on the discussion in #180
Configurable turbo frame identifier via option
I'm thinking of adding an option to the base
DataTableType
that allows setting a turbo frame identifier:By default, that option would be
null
.Turbo frame identifier in data table config and its builder
Following methods could be added to the data table config:
DataTableConfigInterface::getTurboFrameIdentifier()
DataTableConfigBuilderInterface::setTurboFrameIdentifier(string $turboFrameIdentifier)
The base
DataTableType
could use thesetTurboFrameIdentifier
to set the value fromturbo_frame_id
option. If that option equalsnull
, default tokreyu_data_table_
+ name of the data table.Changes in request handler
In
HttpFoundationRequestHandler
, we could check whether the request hasTurbo-Frame
header:$dataTable->getConfig()->getTurboFrameIdentifier()
This would improve DX a lot, suggested by @alexandre-castelain in #180 (comment)
We could add new method to the
RequestHandlerInterface
:This method will return whether the request contains the
Turbo-Frame
header equal to$dataTable->getConfig()->getTurboFrameIdentifier()
, and it should be implemented in theHttpFoundationRequestHandler
.Changes in
DataTableInterface
We could add new method to the
DataTableInterface
:This method would simply call the configured request handler's
isRequestFromTurboFrame
method.Changes in Twig
In Twig, we could introduce a
data_table_turbo_frame_id
method, that simply retrieves an instance ofDataTableView
and returns value of itsturbo_frame_id
variable, for better compatibility in the future.The
base.html.twig
theme should use thedata_table_turbo_frame_id
function.Additional template should be introduced, that simply renders the
data_table
variable for streamed response, explained below:Changes in traits
Probably another trait that works like a
DataTableFactoryAwareTrait
, but withrenderDataTableTurboStream
method (I'm open for better naming):Beta Was this translation helpful? Give feedback.
All reactions