Chapters
46 Chapters
|
4:55:39
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
20.
Multi Controller Communication
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial works perfectly with Stimulus 3!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "1.11.99.1", // 1.11.99.1
"doctrine/annotations": "^1.0", // 1.11.1
"doctrine/doctrine-bundle": "^2.2", // 2.2.3
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.2
"doctrine/orm": "^2.8", // 2.8.1
"phpdocumentor/reflection-docblock": "^5.2", // 5.2.2
"sensio/framework-extra-bundle": "^5.6", // v5.6.1
"symfony/asset": "5.2.*", // v5.2.3
"symfony/console": "5.2.*", // v5.2.3
"symfony/dotenv": "5.2.*", // v5.2.3
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/form": "5.2.*", // v5.2.3
"symfony/framework-bundle": "5.2.*", // v5.2.3
"symfony/property-access": "5.2.*", // v5.2.3
"symfony/property-info": "5.2.*", // v5.2.3
"symfony/proxy-manager-bridge": "5.2.*", // v5.2.3
"symfony/security-bundle": "5.2.*", // v5.2.3
"symfony/serializer": "5.2.*", // v5.2.3
"symfony/twig-bundle": "5.2.*", // v5.2.3
"symfony/ux-chartjs": "^1.1", // v1.2.0
"symfony/validator": "5.2.*", // v5.2.3
"symfony/webpack-encore-bundle": "^1.9", // v1.11.1
"symfony/yaml": "5.2.*", // v5.2.3
"twig/extra-bundle": "^2.12|^3.0", // v3.2.1
"twig/intl-extra": "^3.2", // v3.2.1
"twig/twig": "^2.12|^3.0" // v3.2.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.0
"symfony/debug-bundle": "^5.2", // v5.2.3
"symfony/maker-bundle": "^1.27", // v1.30.0
"symfony/monolog-bundle": "^3.0", // v3.6.0
"symfony/stopwatch": "^5.2", // v5.2.3
"symfony/var-dumper": "^5.2", // v5.2.3
"symfony/web-profiler-bundle": "^5.2" // v5.2.3
}
}
What JavaScript libraries does this tutorial use?
// package.json
{
"devDependencies": {
"@babel/preset-react": "^7.0.0", // 7.12.13
"@popperjs/core": "^2.9.1", // 2.9.1
"@symfony/stimulus-bridge": "^2.0.0", // 2.1.0
"@symfony/ux-chartjs": "file:vendor/symfony/ux-chartjs/Resources/assets", // 1.1.0
"@symfony/webpack-encore": "^1.0.0", // 1.0.4
"bootstrap": "^5.0.0-beta2", // 5.0.0-beta2
"core-js": "^3.0.0", // 3.8.3
"jquery": "^3.6.0", // 3.6.0
"react": "^17.0.1", // 17.0.1
"react-dom": "^17.0.1", // 17.0.1
"regenerator-runtime": "^0.13.2", // 0.13.7
"stimulus": "^2.0.0", // 2.0.0
"stimulus-autocomplete": "^2.0.1-phylor-6095f2a9", // 2.0.1-phylor-6095f2a9
"stimulus-use": "^0.24.0-1", // 0.24.0-1
"sweetalert2": "^10.13.0", // 10.14.0
"webpack-bundle-analyzer": "^4.4.0", // 4.4.0
"webpack-notifier": "^1.6.0" // 1.13.0
}
}
17 Comments
Hello Team,
I basically have the same problem as jmsche (and Kevin-C - but I dont use stimulus-use). As especially jmsches question is not completely answered, I have to post the issue again.
my template:
collection_controller.js:
sortable_controller.js
It fails without any error and looks to me, that the event is not firing. I did the monitorEvents($0) and a ton of other structures of the template. Just can't get it to work. Please help! :/
Hey Eric,
Yeah, communicating between 2 components might be tricky in Stimulus. Because DOM events always goes the next direction: from child elements to parents. In your exacmple you try to fire an event from the parent element so it won't hit the child one But not sure it will work either.. IIRC you fire that in a different direction and it just won't work in native Stimulus way.
But you can always dispatch real JS events like this, and it should always work:
and
This way it does not rely on Stimulus/DOM events and should work well.
Cheers!
Hey Victor,
thanks for your reply. I came up with my structure because I read that
this.dispatch()"releases" the Event from the element where the controller is attached and can be caught at that element or any parent. That's why I putstimulus_action('sortable', 'receivingEvent', 'collection:added')right there. With this structure I triedstimulus_action('...', '...', 'collection:added@window')aswell to catch the event on the "top level", but failed.It dawned on me, that my action is somewhat not working at all and I discovered, that it's not possible to use actions outside the controller scope they belong to. I'm not sure if it is never explicitly mentioned in the videos or if I just missed this part before, but that was my fundamental misconception.
I rearranged my code:
And now everything works as expected.
One last question though: Is there any reason why it would be better (if it's possible) to avoid listening to events on the window-element?
Hey Eric,
Great, glad it works for you! And thanks for sharing the updated working structure :)
Yes, there are some reasons why you may want to avoid global events.
I hope this helps!
Cheers!
Hey @Quentin-D
Have you tried what the docs say? It seems to me that they made part of Stimulus the
useDispatch()behaviorHello Ryan and team,
Having a tough time getting events to dispatch in stimulus. My assumption is that I can simply call
this.dispatch("event:tag"), this I get for free withimport { Controller } from '@hotwired/stimulus';I need not import anything else, is this correct?In my
checkoutcontroller the method that is called upon click viadata-action="click->checkout#choseOne"of<button>is:in the surrounding
<div>of the<button>I havedata-action="checkout:async:chose->plan-detail#displayChoice".This should mean when the event is dispatched, method:
displayChoice(event)is called on theplan-detailcontroller, correct?Well, that's not happening. I used the
monitorEvents($0)trick and saw no event fired. I also do not see the console.log output where I'd expect when thedisplayChoice(event)is called.Thanks for any tips, tricks, and teachings,
Kevin
Hey Kevin,
You only forgot to initialize the "dispatch" functionality. You can do it like this:
Cheers!
Hey MolloKhan,
Thank you, thought I might be missing something like that, so we still have to useDispatch in connect(), but you can call this.dispatch(). Ok, got it. Now I can see the event firing, but the data-action that's doing the listening doesn't seem to be catching the event because that controller method isn't executing. Is there reason you can think of why this wouldn't work in a modal?
Thanks,
Kevin
Hey Kevin,
Can you double-check that you're listening to the right event name? Stimulus prefix the event with the controller's name by default. https://github.com/stimulus-use/stimulus-use/blob/main/docs/use-dispatch.md#reference
Cheers!
useDispatchfromstimulus-useis deprecated, so I use native Stimulusthis.dispatch()function instead.However, the event does not appear in the console. Any way to enable debugging for the native
dispatch()function?Hey @jmsche!
I love that
dispatch()is now just part of Stimulus. About debugging - good question. I haven't usedthis.dispatch()yet... I would hope that the events might show in the console via Stimulus's debugging mode, but it sounds like that's not the case, at least not now.Fortunately, the events it dispatches are normal JS events. So you can use this trick: https://stackoverflow.com/questions/10213703/how-do-i-view-events-fired-on-an-element-in-chrome-devtools
In the console, type:
Then you'll see ALL events that are dispatched. It's a lot, but it should help.
Let me know if that's useful! Cheers!
It may be down to knowledge of js, but after removing "return" keyword and storing fetch Promise in "response" const, and the calling "this.dispatch", how this data are returned from "submitForm" method to "preConfirm" arrow method call?
Hey Peter L.
The main idea of this chapter is to get the response of the AJAX call and dispatch an event that something else (a listener) can handle later. We achieved that by calling
awaitright before the AJAX call. What it does is to resolve the promise and give you the result - it basically makes the process synchronous. After that, we just use thedispatchbehavior to dispatch a custom eventI hope it helps. Cheers!
Hello :)
You say "You can dispatch events without this behavior ... this just makes it easier.". But how could we do without this behavior? :)
Thanks.
Hi! Here's how you can dispatch a CustomEvent as seen in the MDN website, where obj is an HTML element:
In this case, the custom event is dispatches from obj and will bubble up to their parent elements!
"Houston: no signs of life"
Start the conversation!