Buy Access to Course
38.

Ajax Element Reloading Controller

Share this awesome video!

|

Keep on Learning!

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

If we add a new product with valid data - we're going to start selling solar powered flashlights... I got a great deal on them - it does work... but it's not very obvious. You don't see the new product until you reload the page. We could, on success, add a message to the top of the page. That's actually a really great idea. But even if we did, we should also reload the product list so the user can see the new item.

If you think about it, being able to make an Ajax call to a URL... and then use the HTML from that to replace the contents of an element is... kind of a common thing to do. Heck, we basically already do this on the cart page! After removing an item, we make an Ajax call to get the fresh cart list.

So here's the plan: instead of adding more logic to our modal-form controller, which would make it less reusable, let's create a second controller that will make an Ajax call to reload the product list area after a new product is successfully added. To be extra cool - cause we are extra cool - we're going to make this new controller generic so we can reuse it anywhere... like on the cart page!

Bootstrapping the New Controller

Head into templates/product_admin/index.html.twig: the template for the product list page. Let's see. The area that we need to refresh after submit is really just this table. But I'm going to add the new data-controller to the top level div. Let's break it on multiple lines... fix my super old typo on container - no wonder the page didn't look very good - then add {{ stimulus_controller() }}. Call the new controller, how about, reload-content.

71 lines | templates/product_admin/index.html.twig
// ... lines 1 - 4
{% block body %}
<div
// ... line 7
{{ stimulus_controller('reload-content') }}
>
// ... lines 10 - 68
</div>
{% endblock %}

Why are we adding it here... and not directly around the table? Well, in order for the reload-controller to know when a form was submitted successfully, we're going to use an old trick: we will dispatch an event from modal-form controller. To listen to that event, reload-controller needs to live on an element that is around both modal-form controller and the <table> that it needs to update.

Let's go add the new file. In assets/controllers/ create reload-content_controller.js. Steal the entire cart-list_controller - since it's so similar - close it and paste. Add a connect() method with console.log() a refresh icon.

import { Controller } from 'stimulus';
export default class extends Controller {
static values = {
cartRefreshUrl: String,
}
connect() {
console.log('?');
}
async removeItem(event) {
event.currentTarget.classList.add('removing');
const response = await fetch(this.cartRefreshUrlValue);
this.element.innerHTML = await response.text();
}
}

Let's give it a go! Refresh the page, check out the logs and... we are connected!

Creating the Partial Endpoint

To be able to refresh the content of this table, we need an endpoint that returns just the table. To do that, we need to isolate the table into a template partial... like we've done before. Copy the entire table, delete it and then, in this same directory - product_admin - create a new file called _list.html.twig. Paste the table here.

38 lines | templates/product_admin/_list.html.twig
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Description</th>
<th>Brand</th>
<th>Weight</th>
<th>Price</th>
<th>StockQuantity</th>
<th>ImageFilename</th>
<th>actions</th>
</tr>
</thead>
// ... lines 15 - 36
</table>

Back in index.html.twig include that with {{ include('product_admin/_list.html.twig') }}.

37 lines | templates/product_admin/index.html.twig
// ... lines 1 - 4
{% block body %}
<div
// ... lines 7 - 10
>
// ... lines 12 - 31
{{ include('product_admin/_list.html.twig') }}
// ... lines 33 - 34
</div>
{% endblock %}

If we refresh now... so far so good: nothing changes.

Like we did with the new product form, the simplest way to create an endpoint that will return just the table is to make the index action capable of returning a full page of HTML - like it's doing right now - or just the _list template partial.

Let's try the same trick as before. First, add a Request argument - the one from HttpFoundation - and then say $template = $request->isXmlHttpRequest(). If this is an Ajax request, use _list.html.twig. Else, use the template we're using now, index.html.twig. Below, we can replace the index.html.twig with $template.

106 lines | src/Controller/ProductAdminController.php
// ... lines 1 - 15
class ProductAdminController extends AbstractController
{
// ... lines 18 - 20
public function index(ProductRepository $productRepository, Request $request): Response
{
$template = $request->isXmlHttpRequest() ? '_list.html.twig' : 'index.html.twig';
return $this->render('product_admin/' . $template, [
// ... line 26
]);
}
// ... lines 29 - 104
}

Awesome! Now, copy the route name so we can pass this into our controller. We'll use a value. Start by defining that in our controller. Or, really, just rename cartRefreshUrl to just url and it will be a String. I'll also remove the connect() method.

// ... lines 1 - 2
export default class extends Controller {
// ... line 4
static values = {
url: String,
}
// ... lines 8 - 12
}

Pass the value in via the template. So up at the top, on a second argument to stimulus_controller(), set url to path('product_admin_index').

39 lines | templates/product_admin/index.html.twig
// ... lines 1 - 4
{% block body %}
<div
// ... line 7
{{ stimulus_controller('reload-content', {
url: path('product_admin_index')
}) }}
>
// ... lines 12 - 36
</div>
{% endblock %}

Lovely! Before we use that to make the Ajax call, when that call finishes, we're going to need to know where we should put the new HTML. Let's wrap the table in a new div and make it a target: data-reload-content-target="" and... call it content.

39 lines | templates/product_admin/index.html.twig
// ... lines 1 - 4
{% block body %}
<div
// ... lines 7 - 10
>
// ... lines 12 - 31
<div data-reload-content-target="content">
{{ include('product_admin/_list.html.twig') }}
</div>
// ... lines 35 - 36
</div>
{% endblock %}

Go set up that target. At the top of the class, add static targets = [] with content inside.

// ... lines 1 - 2
export default class extends Controller {
static targets = ['content'];
// ... lines 5 - 12
}

Now let's make the Ajax call. Rename the method to refreshContent(). We're not using this method anywhere... but we will soon. Let's see: we don't need to add this class... the value changed to this.urlValue... and instead of using this.element, use this.contentTarget

// ... lines 1 - 2
export default class extends Controller {
// ... lines 4 - 8
async refreshContent(event) {
const response = await fetch(this.urlValue);
this.contentTarget.innerHTML = await response.text();
}
}

As I mentioned, nobody is calling refreshContent() yet. But if they did, it should make the Ajax call and replace the table with the new HTML. So... how will we call this method?

Well, we need to call it after the modal-form controller finishes submitting successfully. So next: let's dispatch a custom event from modal-form_controller and listen to it so that we can reload the content after it's successful.

Once we're done, we'll prove that our new controller is reusable by completely replacing the cart-list_controller with the new one. Yay for less custom code!