Page 1 of 1

How to determine if a Widget is loaded

Posted: Wed Dec 21, 2016 7:53 am
by jyoung
I have a widget that renders a Google bar chart.

After I get some data from the ServerModule, I push that data into the widget to display the chart.

Locally this all works great. When I deployed it to our development IBM i server however, I get fatal error that states
Can't access a widget before it has completed loading
How do I determine if the widget has finished loading?

Re: How to determine if a Widget is loaded

Posted: Wed Dec 21, 2016 8:00 am
by dannyoorburg
You'll have to wait for the Initialize event to fire on your Widget instance...

So whatever code you have now to populate it should probably be in the #MY_WIDGET.Initialize event routine.

Cheers,
Danny

Re: How to determine if a Widget is loaded

Posted: Wed Dec 21, 2016 8:39 am
by jyoung
Thanks for the info on the widget.

I ended up creating a timer to handle it.

First thing I do is define a field to hold whether or not the widget is initialized and capture it in the Initialized event

Code: Select all

define field(#ChartInitialized) type(*BOOLEAN) default(False)
evtroutine handling(#MOBBarChart.Initialize)
#ChartInitialized := True
endroutine
When the ServerModule completes, I check that field, if the chart is not yet initialized, I kick off a timer.

Code: Select all

evtroutine handling(#FindYearlyGrossMargin.Completed)
#SYS_APPLN.TraceMessageText( "FindYearlyGrossMargin Completed" )

if (#ChartInitialized = False)
#WidgetTimer.Start
else
#COM_OWNER.PopulateChart
#COM_OWNER.ToggleChart( True )
endif
endroutine
Then each tick of the timer, I am checking if the chart is initialized.

Code: Select all

evtroutine handling(#WidgetTimer.Tick)
if (#ChartInitialized)
#COM_OWNER.PopulateChart
#COM_OWNER.ToggleChart( True )
#WidgetTimer.Stop
endif
endroutine
I should probably put in a tick count or something to show an error message instead of just waiting.

Re: How to determine if a Widget is loaded

Posted: Wed Dec 21, 2016 9:22 am
by dannyoorburg
I usually use 2 booleans to do pretty much the same thing...

Code: Select all

Define Field(#ChartInitialized) Type(*BOOLEAN) Default(False)
Define Field(#DataLoaded) Type(*BOOLEAN) Default(False)

Mthroutine Name(PopulateChart)

If (#ChartInitialized *And #DataLoaded)

* Populate Chart

Endif

Endroutine
and then just call the method from both event handlers after setting the appropriate boolean. The last event to fire will trigger the actual load...

Danny...