Giter Site home page Giter Site logo

Comments (3)

smnandre avatar smnandre commented on August 20, 2024

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.

gremo avatar gremo commented on August 20, 2024

@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.

smnandre avatar smnandre commented on August 20, 2024

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
Capture d’écran 2024-02-10 à 16 52 01 Capture d’écran 2024-02-10 à 16 52 10

from ux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.