How it works

Client ( your web browser ) send an Ajax request to server.
Then, server return a Json response respecting the good format.
Response format [ { 'action' : 'toast', 'params' : { text: "Lorem ipsum ...", ... } }, { 'action' : 'redirect', 'params' : { value : "umbrella-demo.dev/foo/bar" } }, ... ]
So, Client resolves each actions one by one, calling the right js function depending of action type :
  • action : toast → Toastify({text: 'Lorem ipsum'}).showToast()
  • action : redirect → window.location.href=...
To send ajax request, you must call AjaxUtils.request() on Javascript.
Code Javascript import AjaxUtils from 'umbrella_core/utils/AjaxUtils'; AjaxUtils.request({ 'method' : 'get', 'url' : 'umbrella.dev/foo/bar', 'data' : {}, 'spinner' : true, // display a spinner until server respond 'confirm' : 'Are you sure ?' // open a confirm modal before perform ajax request 'xhr-id' : 'my-id' // prevent multiple ajax request with same id ... // Any other option available with $.ajax method });
Then, your controller must return a JsonResponse.
Code Symfony Controller class MyController extends BaseController { public function myAction() { return new JsonResponse([ ['action' => 'action1', 'params' => ...], ['action' => 'action2', 'params' => ...], ]); // or use js() helper // Your controller must extends Umbrella\CoreBundle\Controller\BaseController return $this->js() ->action1(...params) ->action2(...params); } }
The data-xhr attribute

This attribute will override the default behaviour of link or form and send an Ajax request (with AJaxUtils) to server on click or on submit.
Code HTML <!-- with anchor --> <a data-xhr="umbrella.dev/foo/bar" data-spinner="true" data-confirm="Are you sure ?" href> My link</a> <!-- with form --> <form data-xhr="umbrella.dev/foo/bar" data-spinner="true" data-confirm="Are you sure ?" method="post" > </form> <!-- work with all html element --> <div data-xhr="...."></div>

Example

Modal
Code Symfony Controller class MyController extends BaseController { public function modal() { return $this->js()->modal('@UmbrellaCore/Modal/default.html.twig', [ 'class' => ..., 'title' => ..., 'content' => ..., ... ]); } public function formModal() { $form = $this->createForm(...) return $this->js()->modal('@UmbrellaCore/Modal/form.html.twig', [ 'form' => $form->createView(), 'form_layout' => 'horizontal', // or 'default' ... ]); } }
Offcanvas
Code Symfony Controller class MyController extends BaseController { public function offcanvas() { return $this->js()->offcanvas('@UmbrellaCore/Offcanvas/default.html.twig', [ 'class' => ..., 'title' => ..., 'content' => ..., ... ]); } }
Toast (notification)
Code Symfony Controller class MyController extends BaseController { public function success() { return $this->js()->toastSuccess('Toast text here...', 'Toast title here...'); // or $this->js()->toast('success', 'Toast text here...', 'Toast title here...'); } public function error() { return $this->js()->toastError('Toast text here...', 'Toast title here...'); } }
DOM manipulation
Code Symfony Controller class MyController extends BaseController { public function alert() { // update content of all HTMLElements with selector #alert-stack return $this->js()->updateHtml('#alert-stack', '<div>Your alert ...</div>'); // remove all HTMLElements with selector #alert-stack > * return $this->js()->remove('#alert-stack > *'); } }
Web components (@see documentation)
Code Symfony Controller class MyController extends BaseController { public function toggleMenu() { // def : // return $this->js()->callWebComponent('selector', 'function name', ...function vars); return $this->js()->callWebComponent('[is=umbrella-sidebar]', 'toggle'); } }
Custom
Code Symfony Controller class MyController extends BaseController { public function alert() { return $this->js()->add('alert', ['text' => '...']); } }
Code Javascript <script> umbrella.jsResponseHandler.registerAction('alert', (params) => { alert(params.text) }); </script>

Options

Confirm
Show a confirm modal before perform ajax request :
<a data-xhr="umbrella.dev/foo/bar" data-confirm="Are you sure ?" href></a>
Spinner
Show a spinner until server respond :
<a data-xhr="umbrella.dev/foo/bar" data-spinner="true" href></a>