Comments (3)
As stated in the documentation, emit()
only accept some scalars in the array, so you won't be able to pass "new Notification()" like that (at least not a specific instance)
You can also pass extra (scalar) data to the listeners
So i see two potential options (there may be more):
- if you persist your Notification in any way, you can use ParamConverters as you identified
- you also could play with "hydrateWith / desydrateWith" (see this section in the documentation)
Last "option" / "alternative": if you dispatch this Event from PHP, and only listen to it in another PHP class... you could also use a standard EventDispatcher and avoid a maybe-unwanted roundtrip via the front-end (not sure if applicable in your case, but sometimes it can be a good idea)
from ux.
@smnandre thanks for the idea. Do the hydrateWith / dehydrateWith process occurs also when using #[LiveArg]
? For what I understand from the docs, it seems that the process is used only when passing props to the component....
from ux.
There may be a better way, but i tried something and i believe it suits your needs :
Twig Component
#[AsLiveComponent('Foo')]
class Foo extends AbstractController
{
use DefaultActionTrait;
use ComponentToolsTrait;
#[LiveAction]
public function bar(): void
{
$this->emit('FooBar', ['notification' => new Notification('Hello', 'World!')]);
}
#[LiveListener('FooBar')]
public function onFooBar(
#[LiveArg('notification')] #[ValueResolver(NotificationResolver::class)] Notification $notification
)
{
dump($notification);
}
}
Twig Template
<div {{ attributes.defaults({}) }}>
<button
type="button"
data-action="live#action"
data-action-name="bar()"
>BAR</button>
</div>
Notification DTO
class Notification
{
public function __construct(
public string $title,
public string $message,
) {
}
public function getTitle(): string
{
return $this->title;
}
public function getMessage(): string
{
return $this->message;
}
}
And a little ValueResolver
class NotificationResolver implements ValueResolverInterface
{
public function resolve(Request $request, ArgumentMetadata $argument): array
{
if (Notification::class !== $argument->getType()) {
return [];
}
if (!is_array($attribute = $request->attributes->get($argument->getName()))) {
return [];
}
if (!isset($attribute['title']) || !isset($attribute['message'])) {
return [];
}
if (!is_string($attribute['title']) || !is_string($attribute['message'])) {
throw new NotFoundHttpException(sprintf('The notification for the "%s" parameter is invalid.', $argument->getName()));
}
return [new Notification($attribute['title'], $attribute['message'])];
}
}
And when you click on the button, the live action is called, the event is emitted, and you can get the notification object as you wanted.
Requests | Debug |
---|---|
![]() |
![]() |
from ux.
Related Issues (20)
- [Turbo] InstantClick should not prefetch links from Web Debug Toolbar HOT 11
- [LiveComponent] TemplateCacheWarmer not warming up cache HOT 15
- [Typed] Add a helper or use <template> to provide values HOT 2
- [TURBO] Impossible to redirect with anchor HOT 2
- [LiveComponent] Calling a property using a getter (i.e. computed) doesn't update component state (another component property)? HOT 5
- [TwigComponent] Profiler, memory consumption HOT 13
- The option "extra_options" does not exist. HOT 4
- [LiveComponent] How "defer" option work and the logic behind __invoke HOT 5
- [LiveComponent] getComponent is undefined if the controller loads very quickly as explained in the docs HOT 6
- [LiveComponent] `data-model` started throwing the "Invalid model name" error after upgrading to 2.15 HOT 1
- [LiveComponent] Can I rely on "busy" attribute to show a loading indicator outside a component?
- [Autocomplete] Doesn't load correctly on the first input HOT 1
- [LiveComponent] Updated values not submitted to the server properly HOT 7
- [Site] Add example for LazyImage with `srcset`
- [LiveComponent] Test helper doesn't work with localized components HOT 4
- [LiveComponent] Running `npm install --force` fails with "This is related to npm not being able to find a file." HOT 2
- Explicit re-render of a field when clicking a button HOT 3
- [TwigComponent] Examples in the CVA documentation do not work HOT 4
- [LiveComponent] Using ComponentWithFormTrait and switching forms doesn't keep form values. HOT 3
- Turbo restoration visit displays wrong AJAX HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ux.