Skip to content

Commit

Permalink
persist callbacks in session, add Ajaxcom form type
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanbarlog committed Sep 11, 2017
1 parent f583c9a commit 92fcecd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,4 @@ $this-addFlash(Everlution\AjaxcomBundle\Flash::SUCCESS, 'Your request has been s
- add example for rendering modal
- add twig templates for rendering modal
- add complex usage example

- add FormType documentation
11 changes: 7 additions & 4 deletions src/Ajaxcom.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class Ajaxcom
private $addBlocks = [];
/** @var string[] */
private $removeBlocks = [];
/** @var Callback[] */
private $callbacks = [];
/** @var bool */
private $modal = false;

Expand Down Expand Up @@ -116,7 +114,9 @@ public function renderAjaxBlock(string $id, array $callbacks = []): self

public function addCallback(Callback $callback)
{
$this->callbacks[] = $callback;
$callbacks = $this->session->get(self::AJAXCOM_CALLBACKS, []);
$callbacks[] = $callback;
$this->session->set(self::AJAXCOM_CALLBACKS, $callbacks);

return $this;
}
Expand Down Expand Up @@ -228,9 +228,12 @@ private function addBlocks(Handler $ajax, string $view, array $parameters): Hand
*/
private function addCallbacks(Handler $ajax): Handler
{
foreach ($this->callbacks as $callback) {
$callbacks = $this->session->get(self::AJAXCOM_CALLBACKS, []);
/** @var Callback $callback */
foreach ($callbacks as $callback) {
$ajax->callback($callback->getFunction(), $callback->getParameters());
}
$this->session->remove(self::AJAXCOM_CALLBACKS);

return $ajax;
}
Expand Down
28 changes: 28 additions & 0 deletions src/Form/Type/AjaxcomForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Everlution\AjaxcomBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Class AjaxcomForm.
* @author Ivan Barlog <ivan.barlog@everlution.sk>
*/
class AjaxcomForm extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'attr' =>
[
'novalidate' => 'novalidate',
'data-ajaxcom' => '',
],
]
);
}
}

0 comments on commit 92fcecd

Please sign in to comment.