Is there a method to listen if a custom element triggers a "custom event" from its parent?

Is there a method to listen if a custom element triggers a “custom event” from its parent?

I have a custom-element-a and a state named “value” within that custom element. I don’t want the parent component to directly access this state, thus I want to send a custom “onChange” event if any action in custom-element-a modifies this value.

Nonetheless, I observe in the parent element that I lack a way to listen for a custom event triggered by “custom-element-a.” Is there a method I can employ?

Note that I can currently only listen for the element’s click event.

Sounds like you have a reusable element (custom-element-a) on a page. The page is being described as the parent component.

There is no way for you to set up an event when the reusable element custom state value changes as the page you want to take an action from based on that change in the reusable element’s custom state value is not accessible from the page.

Easy way is to make it so the custom state is on the reusable element itself and not a child element of the reusable element making it so the custom state value of the reusable element is accessible from the page workflows where you can set up a conditional workflow based on the reusable element’s custom state value.

There are more complex ways to achieve the end goal if you are completely against making the custom state value be on the reusable element itself.

Is there are reason you are not willing to put the custom state value onto the reusable element itself?

1 Like

Sounds like you have a reusable element (custom-element-a) on a page. The page is being described as the parent component.

What I mean is that I created a reusable element itself and then defined a “value” state within it. I also generated a custom event with the name “setState” and the parameter “paramVal.” I use this custom event because I don’t want an external component to set the value of the reusable element’s state directly; instead, I want communication to occur via this event.


Fig A: reusable element named “reusable-element” contains state named “value”


Fig B : Custom Event “setValue”


Fig B : “reusable-element” has a Custom Event titled “setValue” to set the state “value” based on its Custom Event Parameter titled “paramVal.”


Fig C : An internal action (clicked button) within the reusable element that modifies the state with a formula.

There is no way for you to set up an event when the reusable element custom state value changes as the page you want to take an action from based on that change in the reusable element’s custom state value is not accessible from the page.

I see …

Easy way is to make it so the custom state is on the reusable element itself and not a child element of the reusable element making it so the custom state value of the reusable element is accessible from the page workflows where you can set up a conditional workflow based on the reusable element’s custom state value.

Yes, the explanation is actually rather simple: I want the reusable element to be encapsulated, I don’t want the state to be immediately accessible from the outside, and I only want communication into and out of the reusable element to occur via events (custom events). This is because it simplifies sending data into and out of elements with a single action.

Consider, for example, a reusable element calculator that requires three states, stateOne, stateTwo, and stateResult. To populate the stateOne and stateTwo from the Page, you can either use the setState action as depicted in figure 1 or trigger the setValue event of the reusable element as depicted in figure 2.


Above Fig 1 : directly setting the state of reusable element.


Fig 2 : calling custom event in reusable element to setValue

The implementation of the second technique utilizing events is simpler because the user is aware of the parameters to send to the reusable-element and only needs to take one action. This provides a sort of API contract in which two parameters are required when calling setAction.

While the second way requires at least two actions, Page does not know which state of the reusable element should be modified.

Up until this point, this truly meets my requirements. Now we have reusable-element with setValue API contract.

However, my next issue is how to tell Page that the state of the reusable-element has changed. Because the page cannot obviously listen for custom events from within the reusable element.

This solution has weaknesses: Page must know the state of this reusable element it requires. How does Page determine that stateResult is the desired output?

In the meantime, if Page can listen to the custom-event “OnResultChange” from the reusable-component, it will be able to retrieve the required results from the onResultChange parameter.

Is there a flaw in my assertion, or is there something I do not know?

Make it so the custom state is on the reusable element itself, which the page has access to.

I asked this, because the easiest solution is to put the custom state on the reusable element itself so the page can access it…also reduces the need for the entire setup of custom workflows, because in the end the page is where the custom workflow is triggered anyway, so it is still in fact accessing the custom state value on the reusable element, just in a more round about way.

1 Like

The clarity is appreciated. :slightly_smiling_face:

Hi.

I’m having the same issue as arif, and I’m not being able to do anything about it, because I don’t see any way to catch any event from the reusable element in the page, not even the “When clicked” event.

This is my elements tree, where _tab_control A is a reusable element:
imagen

I create a new event in the page:
imagen

But the reusable element is not in the list of elements to select:
imagen

It seems that there is no way to listen to any event triggered from the reusable. Is that so?

Regards.

1 Like