how to get event type that is fired - apache-flex

I am working on Flex project these days and having java background. In my current task, same pop up is opened when two different types of custom events are being fired and I have to hide a button for one event type. So, how i can get event type.
Thanks in advance.

The Event class has a property called type wich is a string stating what kind of event it is.
function eventHandler(event:Event):void {
trace(event.type);
}

Generally speaking, you can get it this way:
mc.addEventListener(MouseEvent.CLICK, myMethod);
function myMethod(evt){
trace(evt.type);
}

Related

How to add a click event handler to a wxStaticBitmap component?

I want to add click event handler to a wxStaticBitmap component.
I use the following statement to create a dynamic event handler:
Connect(ID_STATICBITMAP1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&MyFrame::OnStaticBitmap1Click);
In OnStaticBitmap1Click(), I just want include the following function call:
wxMessageBox(_T("Hello World"));
However, obviously, my code doesn't work. When I click on the static bitmap, nothing happens.
Can anyone tell me why? Any advice would be appreciated.
A wxBitmapButton issues an wxEVT_COMMAND_BUTTON_CLICKED event.
A wxStaticBitmap does not.

Flex AsyncToken for listening to Alert box buttons

Can I listen to Alert button click between components using AsyncToken?
Basically, I want to have a method that opens an Alert with buttons and have it return an AsyncToken so that other components calling that method can listen for button click.
Example:
var token:AsyncToken=methodThatOpensAlert();
token.addResponder(new mx.rpc.Responder(buttonClick));
What's the way to do that?
Thank you.
You might be able to use an AsyncToken to achieve this but you could also just register for custom events that you dispatch from the pop up, this is a much cleaner method IMO. Really you've got two relatively clean options I can think of. 1 you make your pop-up dispatch events like "okClicked" "cancelClicked" for different button clicks within the pop-up, you create an instance of the pop up and add listeners then call PopUpManager.addPopUp, or else you do PopUpManager.createPopUp and keep a reference to the returned display object (the instance of the pop-up it created) and add your listeners then. 2 you make two properties in the pop up typed as function, you use them as call backs, so when you create the pop-up you set the okClickedFunction and cancelClickedFunction (or whatever your buttons may be) then in the pop-up you put cilck handlers on the buttons, check to see if the appropriate call-back function is set and call it if so, like
if(okClickedFunction)
okClickedFunction();
Let me know if you have a specific need that makes you think you must use the AsyncToken, but from checking out the docs it looks as though it's strictly meant to work with the other RPC methods and lots of properties are read-only.
EDIT:
[SomeUtilClass.as]
private static function methodThatOpensAlert():CustomAlert
{
return PopUpManager.createPopUp(Application.application, CustomAlert) as CustomAlert;
}
[CustomAlert.as]
[Event(type="flash.events.Event", name="button1Clicked")]
[Event(type="flash.events.Event", name="button2Clicked")]
private function button1Clicked_handler(event:MouseEvent):void
{
dispatchEvent(new Event("button1Clicked", true));
}
private function button2Clicked_handler(event:MouseEvent):void
{
dispatchEvent(new Event("button2Clicked", true));
}
[ThingThatUsesAlert]
var ca:CustomAlert = SomeUtilClass.methodThatOpensAlert();
ca.addEventListener("button1Clicked", button1ClickHandler);
ca.addEventListener("button2Clicked", button2ClickHandler);
And I believe mouse events bubble by default anyhow still so you could really just listen for a click event on the pop up then use the event.target to determine if it was one of the buttons your interested in.
Let me know if you can make sense of this or need more info.

Flex Mobile Back Button

I search all through the internet and all topics about taking control of the back button in views are saying same thing:
override protected function backKeyHandler():void
{
//Block native 'back' behavior.
}
But when I write this code into my views I always taking same error:
1020: Method marked override must override another method.
I look for this but didn't find a solution.
If the method isn't defined in some parent; then it can't be overriden. Just remove the 'override' keyword from your method:
protected function backKeyHandler():void
{
//Block native 'back' behavior.
}
This method will do nothing unless you add an event listener for it to be called. If you're using an MXML View, you can add your listener to the backKeyPressed event:
<s:View backKeyPressed="backKeyHandler()">
</s:View>
I'll add that this is for mobile applications only.
I can't comment on specific code you found on the Internet without actually knowing what that code or documentation was.
A good article about Back button issue: http://theorynine.com/labs/taking-control-of-the-back-button-in-views-adobe-air-flex-mobile/

Actionscript 3: Why override custom event...and when to override it?

I always have a question about override custom event. I am not sure why or what do to inside override function. I searched google but didn't get too many feedbacks. I appreciate if someone can light me up. Thanks.
EDIT:
My projects seem work fine even though I use my custom event without override. Anyone could explain it? Thanks.
In general, you need to override a function when its use needs to be modified. So for instance, say I have a class called Car. In this class I have a function called go() which starts the car.
Now if i extend this class into another class called PickupTruck, I need to override the Car class' go function so it not only starts the car, but also attaches the Trucks trailer.
So in your case you have to override the clone method of your CustomEvent class because it should return a new CustomEvent instead of a new Event.
From the docs:
When creating your own custom Event
class, you must override the inherited
Event.clone() method in order for it
to duplicate the properties of your
custom class. If you do not set all
the properties that you add in your
event subclass, those properties will
not have the correct values when
listeners handle the redispatched
event.
So, you can have a problem if you don't override clone and redispatch an Event. Also, the problem is not only that the custom properties will not be copied. The clone method will be called on the base class, Event. This will return an Event object, not a CustomEvent. If you have a handler that expects a CustomEvent and receives an Event, an error will be thrown and the code in your handler will not run.

Avoid FocusOut event trigerred when apllication loses focus

I have done an override of the standard TextInput component
In this component I have :
addEventListener( FocusEvent.FOCUS_OUT, handleFocusOut );
My method is triggered when the field loses focus for another field (nice)
Problem : It is triggered also when the whole flex application loses focus (when my field has the current focus inside my form)
Questions :
What have I done wrong ?
Is there a way to avoid doing stuff when it is a application-focusout-event ?
Regards
I am not sure why this is behaving this way. But one solution might be to have an eventListener for FOCUS_OUT event at the application level and call stopImmediatePropagation().
Thanks for your help !
Here is what I have done in my component (textinput child)
Add two event handler :
- addEventListener( Event.ACTIVATE, handleEventActivate );
- addEventListener( Event.DEACTIVATE, handleEventDeActivate );
They update the internal field _isApplicationActive
I handle the focusOut event :
addEventListener( FocusEvent.FOCUS_OUT, handleFocusOut );
in the method I have
if (!_isApplicationActive) { event.stopImmediatePropagation(); }
With this my focusOut handling functions are not called when the app is deactivated
Because => DECACTIVATE events are called before FocusOut events !
The easy answer is to check if event.relatedObject (the object receiving focus) is null. Flex's FocusManager tries really hard to ensure some flex object has (flex) focus, so it should not be null otherwise.
Do also check isRelatedObjectInaccessible if you might need to.

Resources