How to close popup when screen changes?

In addition to the quick-and-dirty avenue I already supplied, there's two other ways to do this.

More correct:
In the onShutdown Event script for the View in question, close out all "known" Popups for the View.

known_popup_ids = ["a", "b", "c"]
for popup_id in known_popup_ids:
    system.perspective.closePopup(id=popup_id)

This requires you to always update the list of potential Popups, so this is inherently brittle as you develop and add more content. Additionally, there is always the possibility of other Views (Popups, Docked Views) opening their own Popups, and this View would know nothing about them.

Most correct:
In the onShutdown Event script for the View in question, broadcast that all Popups should be closed for the page.

system.perspective.sendMessage(messageType="CLOSEPOPUPS", scope="page")

This route requires that EVERY POPUP have at least one component which has a configured Message Handler listening at the page scope for a "CLOSEPOPUPS" message. It also requires that the Popup either knows its own ID (passed in as a parameter perhaps, or hard-coded as a custom prop). You are not able to rely on supplying an empty-string id because there could be other Popups which would be closed.

system.perspective.closePopup(id="MyKnownId")

This route is the "most correct" because there is no list of Popups to manage as development continues, and it allows the individual Popups to manage their own lifecycle. Also, this route will also act on Popups opened from other Views as long as they too have a configured Message Handler and ID.

8 Likes