Create Custom Events that Emit to a Fragment's Parent Container
One way to pass data from a fragment to its parent container (say, a page or another container like a different outer fragment) is by raising custom events that "emit" to the container.
Let's extend the employee contacts use case to see how to do this. Here, the contacts fragment lets you specify whether email is an employee's preferred means of communication. A user who toggles the Emails Allowed? switch in the fragment will see a notification that their communication preference has been set to email:
Description of the illustration fragment-data-email-scenario-test.png
Behind the scenes, when the user toggles the Emails Allowed? switch, an action chain defined on the fragment fires an event that "emits" the event's payload to the fragment container. An event listener on the fragment container, watching for this fragment event to fire, triggers a page-level action chain to perform some action—which, in our example, is firing a notification that the selected employee's email preference has been set.
- Configure your fragment to raise a custom event that the parent container listens to.
- Configure your page's fragment container to receive and process the fragment's custom event.
- Now click the Preview icon in the header, select an employee, and toggle the Emails Allowed? switch. You'll see a message that the employee's email preference is set.