The CXAObservable service
The CXAObservable service provides the necessary support required for renderings to communicate and still remain loosely coupled, independent and unaware of other renderings in use. The basic concept is similar to the Observer design pattern except that it is not an Observable object that changes state but individual renderings that can trigger an event through the Observable, which other renderings are listening for. A rendering subscribes to events and when the event is emitted by another rendering, the subscriber is notified of the event through CXAObservable.
Internally, the CXAObservable
service uses an implementation of CustomObservable
to subscribe and emit using an instance of the observable. To support more complicated scenarios, you can swap out this implementation with other implementations such as RxJs Subject.
Communication between the renderings is managed during rendering initialization. For example, if any event is emitted before all the renderings are initialized (CXAApplication.IsInitialized
= false), which could happen if a rendering emits an event during initialization, the event is held in the queue until all renderings have been initialized. The events in the queue are emitted in sequence. This helps prevent a missed event by the subscribers such as renderings that subscribe to an event after another rendering has emitted the event.
The CXAObservable service performs the following methods. It interacts with CXACartContext and uses various shared events.
Method |
Description |
Code |
---|---|---|
Subscribe to an event |
A rendering subscribes to the event to perform a method. The message parameter used depends on what is passed in when the The listener is called with the event argument. It handles the argument based on the type of object emitted for the event. This method returns the subscriber. |
RequestResponse
where:
|
Emit an event |
Rendering emits an event, which can be a string or any type of object. Best practice is to have a consistent type for each event that can be handled by all listeners. |
RequestResponse
|
Subscribe to a product |
Rendering subscribes to all events associated with a specific product ID in order to perform a method. The listener is called with two arguments:
Returns the subscriber. |
RequestResponse
where:
|
Subscribe to a product event |
Rendering subscribes to the event associated with the specific product ID to perform a method. The listener is called with the event argument. |
RequestResponse
where:
|
Unsubscribe from an event |
Rendering unsubscribes from an event. |
RequestResponse
where:
|
SXA Storefront uses the following shared events.
Event |
Description |
Methods |
---|---|---|
|
Requests the Message Summary rendering to remove displayed messages. |
Listening
Emitting
|
|
Requests the Message Summary rendering to display the message. Event argument: RequestResponse
|
Listening
Emitting
|
|
Emits the event when the latest cart is received. This event is emitted by the CXACartContext whenever the object received by the context is of type CartJsonResult. |
Listening
Emitting
|
|
Requests the Product Price rendering to display the price of the current product variant or product bundle selection. Event argument: RequestResponse
|
Listening
Emitting
|
|
Emits an event when a product variant is selected. RequestResponse
|
Listening
Emitting
|
|
Emits an event when a product bundle is selected. Event argument: RequestResponse
|
Listening
Emitting
|
|
Emits the event to indicate that the current product selection is valid so that the Add to Cart rendering can enable the Add to Cart button. |
Listening
Emitting
|
|
Emits the event to indicate that the current product selection is valid so that the Add to Cart rendering can disable the Add to Cart button. |
Listening
Emitting
|
|
Emits the event to indicate that the current bundle selection’s stock status is valid so that the Product Bundle can then request for the Bundle Selection’s price. Event argument: RequestResponse
|
Listening
Emitting
|
|
Product Inventory rendering emits this event when the response is received from the API endpoint Event argument: RequestResponse
|
Listening
Emitting
|
|
The CXACartContext service emits the event after calling to the API endpoint |
Listening
Emitting
|
|
The CXACartContext service emits the event after calling to the API endpoint |
Listening
Emitting
|