plasmic icon indicating copy to clipboard operation
plasmic copied to clipboard

Event listeners with REST API

Open andrewlevada opened this issue 3 years ago • 6 comments
trafficstars

I am trying to add an onclick listener to button on the page. Also, I am using REST API to fetch html from Plasmic. In componentProps query documentation there is a line:

This should be the props to be passed to the component, with this you can customize which variants are going to be active, define the value of expected props and override slots content (with text only).

And indeed, when I try to pass function as a prop to api, it fails with code 500. Does that mean that it is not possible to use event listeners with plasmic rest api?

andrewlevada avatar Feb 07 '22 13:02 andrewlevada

That's right, there's no way to specify event listeners via the REST API. It's unclear what this would mean. For something like React, you can do this, since you are working within a JavaScript runtime, can pass enclosures that can actually interact with other parts of your application, etc. What are you looking for here?

yang avatar Feb 08 '22 23:02 yang

That's unfortunate. I am trying to combine Plasmic and native Web Components (for easier implementation I use Lit). So I've created a simple component, which fetches html from Plasmic via REST and renders it. It works fine, but I guess if adding event listeners is not possible I'll have to do it theother way. Basicly, I want to use full functionality of Plasmic without any framework like React. Is this possible?

andrewlevada avatar Feb 09 '22 05:02 andrewlevada

Update here. Eventually I've implemented simple querySelector("button[class*=nameOfComponentInPlasmic]") call after html from plasmic is loaded. Works ok so far, but it defenetly would be nicer to have some easier and less error-prone approach to adding event listeners with REST.

andrewlevada avatar Feb 22 '22 17:02 andrewlevada

Closed by accident :(

andrewlevada avatar Feb 22 '22 17:02 andrewlevada

Hi @andrewlevada! 👋

Have you try calling with the hydration option?

According to the doc, it looks like what you would like to achieve:

"Since event listeners aren’t going to be attached when only the API result is used, you may have to perform hydration, this way your pages will become responsive to user interaction. You can indicate it with the following query params."

arenzel avatar Mar 16 '22 09:03 arenzel

@arenzel thanks for the response. Unfortunately, I couldn't find a way to make hydration of specifically event listeners work with rest api. @yang also pointed out that this is in fact intended.

I don't think that behavior I want to achieve is possible, unless there is a way of manually calling hydration function on client after getting code with rest.

andrewlevada avatar Mar 16 '22 09:03 andrewlevada