Setting variables during component initialization in Flex - apache-flex

I am loading a component which makes a HTTPService call to get data that will then be used to set certain variables in the component. I make the HTTPService call in an init() function (for the initialization event) and then set the variables according to the data received in the HTTPService result handler. However, the variables are still null at both the initialize stage and at the creationComplete stage. If I try and read the variables in a creationComp() function (for the creationComplete event), those variables are still null. Is this correct?
I guess I don't understand the flex initialization cycle very well. When are those variables actually set and available to be used? I need to manipulate those variables automatically after the component loads. Is there an event that comes after creationComplete that is appropriate or some other way to approach this? I am using Flex 3.

Your understanding of the Flex component lifecycle is correct; initialize event fires before creationComplete.
However, an HTTPService call is a separate asynchronous operation. The result handler is not guaranteed to be called by the time the creationComplete event fires. You should do the manipulation of the variables in the result handler instead.

You should think about preventing the creationComplete event being dispatched from your component until the HTTPService has returned, then manually dispatch the event yourself.
That would sort out your timing issues.

Related

Event propagation in Qt

I've just the documentation on the Qt event system and the QEvent class. I'm interested in the behavior of the QObject::event() method. The documentation states:
This virtual function receives events to an object and should return true if the event e was recognized and processed.
What is the expected behavior when false is returned from the event() method? What else is attempted in order to handle the event? Is the event automatically forwarded to the parent object?
Note: I know the source is available, and I do have a copy. I'm ideally looking for some piece of documentation addressing this behavior.
I believe the best practice is to explicitly forward the events to the base-class event method if you do not wish to filter that event type (e.g. return QObject::event(event);) since the event function delegates events to specific handlers (e.g. QWidget::keyPressEvent).
QCoreApplication::notify propogates events based on the return value. On true, it considers the event as consumed and stops. Otherwise, the event is passed to the object's parent. For more information, see Events and Filters and Another Look at Events.
Some Events can be propagated.Event will be propagated to it's parent and it's parent recursively until it is processed. Take a look at this:https://doc.qt.io/archives/qq/qq11-events.html

How to determine currently processed event in ActionScript?

When one call Clipboard.generalClipboard.getData() in ActionScript outside of Event.PASTE processing function it fails with following message
The Clipboard.generalClipboard object may only be read while processing a flash.events.Event.PASTE event.
Even if I dispatch Event.PASTE event and call this function within event handler it still fails.
How does it determine which event is currently being processed?
you need to lister for the real paste Event from the stage and not simulate it. This is a security issue so flash apps do not read your global clipboard data, only on user paste action.

Flex app to load data before anything else

I have a flex app that takes data from a back end database then displays the content in one of 3 views.
These views are all in a viewstack which is instantiated in main.mxml
The method to get the data (remote object)is also in main.mxml.
The views rely on the data so how can I go about making sure that the data is loaded first before any of the views in viewstack are created / initialised to stop me having null reference errors?
When you get the data, you should have a callback function defined to receive that data (callback function is the function you put into addEventListener). You just need to call the function to create your viewstack after all your callbacks have been called.
The way I would do it is create an class field called numCallbacks. Increment this variable everytime one of your callbacks is called. Right after you increment it, check if numCallbacks == the number of callbacks you have. If true, create your view stack.

Flex - Why is my custom event not being registered with the following event listener?

printableInvoice.addEventListener(batchGenerated, printableInvoice_batchGeneratedHandler);
Results in this error:
1120: Access of undefined property batchGenerated. I have tried it as FlexEvent.batchGenerated and FlashEvent.batchGenerated.
The MetaData and function that dispatches the even in the component printableInvoice is all right. It I instantiate printableInvoice as an mxml component instead of via action-script it well let put a tag into the mxml line: batchGenerated="someFunction()"
Thanks.
batchGenerated should be a string.
It looks like your application dispatches an event whenever the batch is generated.
I'm assuming inside your code you have something along the lines of either:
dispatchEvent( new BatchEvent("batchGenerated") );
or
dispatchEvent( new BatchEvent(BatchEvent.BATCH_GENERATED) );
The second way is usually preferred as using variables instead of magic strings gives you an extra level of compile time checking.
The first required parameter of events is typically the type of the event - Event.CHANGE (aka "change"), FlexEvent.VALUE_COMMIT (aka "valueCommit") etc.
This is what the event listener is actually comparing against.
So in your event listener code above, you would want to change the line to be either:
printableInvoice.addEventListener("batchGenerated", printableInvoice_batchGeneratedHandler);
or hopefully
printableInvoice.addEventListener(BatchEvent.BATCH_GENERATED, printableInvoice_batchGeneratedHandler);
If you want to go further, the Flex documentation goes into some detail as to how the event system works, and how the events are effectively targeted and handled through the use of the Capture, Target, and Bubble phases.

Do superfluous calls to addEventListener("event", thisSpecificFunction) waste resources?

I have ItemRenderers that need to listen for events. When they hear an event (and when data changes), they dispatch an event with their current data value.
As item renderers are reused, each of them is going to add its callback in set data(value...)and pass the callback function in the event as well as the current data value.
So, the listener of the item renderer's bubbling event will set someEventDispatcher.addEventListener("someEvent", itemRendererEvent.callbackListener). This will happen more than once.
Does setting the same event listener on the same event for the same dispatcher waste resources? Does the displatcher see that it already has the listener?
I believe other than the initial call to add the listener, it doesn't waste resources.
However it's a good idea not to add any listeners unnecessarily, and it's also a good idea to keep any sort of business logic out of getter / setters, but that's strictly for the sake of code legibility.

Resources