Passing reference to component to a window

I’m copy pasting my personal notes, but perhaps you’ll find them useful.
Here:

Using callbacks in Vision

Window - Caller

  • Can be in the script of a button.
  • The callback can reference event or self depending on the context in which this script is running (within a custom method, or a component event).
def callback():
	print 'hey'

window = system.nav.openWindowInstance('Popups/popupName')
window.putClientProperty('callback', callback)
system.nav.centerWindow(window)

Popup - Callback

window = system.gui.getParentWindow(event)
callback = window.getClientProperty("callback")
callback()

Questions

  • What happens if client property isn’t defined? I think it just returns None.
  • What happens if calling window isn’t opened anymore?
    • Use weakref(s) if that’s a concern.

Using weakref

Note:
Weakref is a python module to add to folder user-lib\pylib of Ignition:
It avoids breaking garbage collection in some cases.

# -----------------
# DUBEG:
# For whatever reason, the weakref must be created
# within a built-in component event (eg. propertyChange, actionPerformed), 
# and not within a custom/extension method, otherwise it won't work (weakref returns None).
# Not sure why, but probably related to the way extension methods are handled.
# You can always pass the callback function as an argument to a custom method, and that will work.
# -----------------
import weakref

textbox = event.source.parent.getComponent('tbResult')
textboxRef = weakref.ref(textbox)

def callback():
	o = textboxRef()
	if o is not None:
		o.text = "Callback :)"

window = system.nav.openWindowInstance('Popups/ppuConfirmAction')
window.putClientProperty('callback', callback)
system.nav.centerWindow(window)
2 Likes