Communicating between Parts

Considering a Potomac application is composed of a collection of parts, its quickly becomes apparent that parts will be required to talk to each other. All communication between parts is facilitated by events.

The primary use-case for interpart communication is partA whose content is relative to the selected item in a control within partB. These selection-based use-cases should utilize the SelectionService and associated part events.

Part Selection

If a part want to enable other parts to listen to its selection, the part should fire selection changed PartEvents.

Example of part firing selection changed events:

public var tree:Tree = ...;
 
[Handles(source="tree",event="change")]
public function onTreeSelectionChange(e:Event):void
{
     PartEvent.sendSelectionChanged(this,tree.selectedItems);
}

When a selection changed PartEvent is dispatched from a part, Potomac dispatches a new SelectionEvent on all parts. Therefore, a part simply needs to add a SelectionEvent handler to respond to these selection events.

Example:

[Handles(event="partSelectionChanged")]
public function onPartATreeChange(e:SelectionEvent):void
{
     //filter out selection events we're not interested in
     if (e.partReference.id == "partA")
     {
          //look inside e.selection
     }
}

The only situation where a part will need to get a reference to the SelectionService object is when there is a delay between its creation and the part its listening to’s selection. In other words, if the part needs to know what the current selection is because it wasn’t created when partA’s selection was changed. In these cases, partB can inject the SelectionService and call SelectionService#getSelection().


Other Part Communication

All other part communication can be accomplished through part event broadcasting.

Potomac enables a part to easily broadcast an event on all parts on all pages by simply sending a partBroadcastToParts PartEvent. This broadcast event takes as a parameter the event that the part wishes to broadcast.

Example:

PartEvent.sendBroadcast(this,new Event("myCustomEvent"));

Since these events are broadcast on all parts, a part can easily use [Handles] again like:

[Handles(event="myCustomEvent")]
public function onMyCustomEvent(e:Event):void
{
   ...
}