Async behaviours
This document aims to explain various techniques and implementations of async behaviours in your frontend application.
#
Basic async fragment replaceLet's say you have a component in src/components/ProductDetail/
that you want to inject asynchronously into a route.
In your route template, e.g. in /src/routes/myroute/index.blade.php
write:
In your route js, e.g. in /src/routes/myroute/index.js
:
Done, the component is automatically fetched and placed inside your template.
note
You need to import the js/scss of your component either in the route from where you requested it or asynchronously with dynamic imports.
#
Altering a component when used as a fragmentYou might find useful to use the same route or component both in a fragment way (asynchronous) and in standard way (synchronous). A common use case is a product page whose inner content is also used inside a product dialog component reused across various routes. In this scenario you can differentiate the template and selectively exclude or include parts of it based on whether the template is used in a fragment or not. For this purpose a custom blade directive is made available by the FragmentsServiceProvider.
Continuing from the above example your template in src/components/ProductDetail/index.blade.php
could look like:
#
Customize fragment loading stateUsing a progress core component:
Using a skeleton core component:
#
Comunicate with your internal frontend endpointsThree steps to expose an ajax endpoint
- Create a route controller in
/src/fragments/MyController.php
and define all the methods you need:
- In
/src/fragments/routes.php
define routes endpoint and assign the controller you just wrote.
- Consume the endpoints in your JavaScripts using the ajax utilities:
The laravel related ajax calls always sends along the X-CSRF-TOKEN
in the request header for security reasons.