Perspective: Cannot update label with popup response text

In perspective, I have a view with a button and a label (“Label_0”). The button “onClick” event script is as follows:

def runAction(self, event):
	def func():
		system.perspective.openPopup('myPopup', 'Application/confirm', id = 99, params = {'id':99 }, showCloseIcon = False, modal = True, draggable = False)

		if self.session.custom.popup_response == "CANCEL":
			self.getSibling("Label_0").props.text = "XX"
		else:
			self.getSibling("Label_0").props.text = "OO"
	
	func()

The “Application/confirm” view has two buttons (“OK” and “CANCEL”), whose code is as follows:

# script for "OK" button
def runAction(self, event):
	self.session.custom.popup_response = "OK"
	system.perspective.closePopup(id = self.view.params.id)
# script for "CANCEL" button
def runAction(self, event):
	self.session.custom.popup_response = "CANCEL"
	system.perspective.closePopup(id = self.view.params.id)

Now, if I run the code, the popup shows, but it appears that the text of “Label_0” is updated.with the desired text only when I reopen the popup, not when it closes.
Sample sequence: I open the popup, choose “OK”, the popup closes, Label_0 is not updated. I open the popup again, now I can see (shaded under the popup) Label_0 now showing “OO”. I choose “CANCEL”, the popup closes, and Label_0 still shows “OO”. I reopen the popup, and now Label_0 changes to “XX”. Etc. etc.

Am I doing something wrong?

Yes.

It’s important to note that Perspective Popups do not behave like Vision Popups. There is no “response” from the Popup. The script which opens your Popup opens the Popup correctly, but then IMMEDIATELY checks the status of a session property before a user could possibly interact with the Popup, so when you check session.custom.popup_response, you’re going to obtain whatever value it had before the Popup was opened - not the value based off of the current user interaction.

I recommend removing the if/else block from your code, and binding the text for Label_0 to the session property in question while using a script transform like this:

default_text = ''  # what value do you want the Label to have before a user confirms or cancels?
if value:
    if value == 'OK':
        return '00'
    if value == 'CANCEL'
        return 'XX'
return default_text

Keep in mind this value is retained for the life of the session, so if a user visits this View again the session property will still have a value and so the Label will still display the last value supplied.

Unfortunately, the above is a simplified reproducer of the issue. The actual code has, instead of the “Label_0” update, a “return” statement that is supposed to return the chosen value to another function in another part of the code (that does or doesn’t carry on with its job depending on the user’s response to the popup). Is there a way to get this behavior (“Vision-like”, if you want) with Perspective?

There is no way to have your code “wait” on a response form a Popup because the Popup will never return anything.

What I usually recommend when users use Popups is to familiarize yourself with system.perspective.sendMessage() (docs). Have the popup buttons invoke a sendMessage call, and have the main view listen with a Message Handler. You can then have the main view take no action based on the Popup until a user has actually performed an action within the Popup. This, in essence, causes the View to wait for a response - as opposed to your script.

1 Like

I used sendMessage in the past so I know it, adapting this case to the message paradigm will require a significant code refactoring, but if it’s the only way then so be it. Thanks!

1 Like