PUBLISH/SUBSCRIBE example
- Last Updated: April 19, 2024
- 3 minute read
- OpenEdge
- Version 12.8
- Documentation
The h-CustOrderWin6.w procedure runs a separate window called h-OrderWin.w to display details from the current Order. You can run this Order window multiple times.
To see how you can use PUBLISH and SUBSCRIBE statements to extend the communication between these procedures:
- Open h-OrderWin.w and save it as h-OrderWin1b.w.
- In h-OrderWin1b.w define this internal procedure called
ShipDateChange:/*--------------------------------------------------------------------- Procedure ShipDateChange: Purpose: receives the event of the same name to update the ShipDate field display when the field value changes. Parameters: Order Number and new ShipDate SCREEN-VALUE. Notes: ShipDateChange is PUBLISHed by h-CustOrderWin6.w ---------------------------------------------------------------------*/ DEFINE INPUT PARAMETER iOrderNum AS INTEGER NO-UNDO. DEFINE INPUT PARAMETER cShipDate AS CHARACTER NO-UNDO. IF STRING(iOrderNum) = Order.OrderNum:SCREEN-VALUE IN FRAME OrderFrame THEN DO: Order.ShipDate:SCREEN-VALUE IN FRAME OrderFrame = cShipDate. shipDate:BGCOLOR = dateColor(PromiseDate, DATE (ShipDate:SCREEN-VALUE)). END. END PROCEDURE.This code responds to an event published by the main window whenever the value of the ShipDate field changes. It receives both the Order Number and the new ShipDate as
INPUTparameters. If the Order Number matches the one displayed by this instance ofOrderWin, then the displayed ShipDate is changed to the value passed in, and thedateColorprocedure is run to flag it with a new background color if its value requires this warning signal. - Add this
SUBSCRIBEstatement to the main block of h-OrderWin1b.w:ASSIGN THIS-PROCEDURE:PRIVATE-DATA = STRING(OrderNum) hSource = SOURCE-PROCEDURE shipDate:BGCOLOR = dateColor(PromiseDate, ShipDate). SUBSCRIBE TO "ShipDateChange" IN SOURCE-PROCEDURE. IF NOT THIS-PROCEDURE:PERSISTENT THEN WAIT-FOR CLOSE OF THIS-PROCEDURE.This sets up every running instance of h-OrderWin.w to receive the
ShipDateChangeevent when theSOURCE-PROCEDURE h-CustOrderWinpublishes it. There might be multiple instances ofOrderWinthat have subscribed to the event. Under other circumstances, there could just as easily be multiple, completely different versions of the procedureShipDateChangein multiple different procedure files that do different things with the changed ShipDate information. The publisher has no reason to know or care. - In h-CustOrderWin6.w, go into the property sheet for the OrderBrowse and enable the ShipDate column.
- Define this
LEAVEtrigger for the ShipDate field:DO: IF Order.ShipDate NE DATE(Order.ShipDate:SCREEN-VALUE IN BROWSE OrderBrowse) THEN PUBLISH "ShipDateChange" (INPUT Order.OrderNum, INPUT Order.ShipDate:SCREEN-VALUE). END.If the field value changes, the published event notifies all subscribers.
- Run the main window and click the Order Details button several times for different Orders to bring up multiple instances of OrderWin.
- Modify the ShipDate for one of those
Orders, then tab out of the field.
The corresponding
OrderWininstance for that Order shows the new ShipDate, possibly with a different color, depending on the value of ShipDate, as this sequence shows:- Run h-CustOrderWin6.w and click the Order
Detail button for several Orders:
One of those orders shows that there is no ShipDate:
- Enter a ShipDate for the Order, then tab out
of the field:
Now the Order Detail window for that Order shows the new date:
- Run h-CustOrderWin6.w and click the Order
Detail button for several Orders: