I would recommend a pattern like this:
<mx:Application>
<mx:Script>
<![CDATA[
private function onClick(event:Event):void{
trace("click");
myButton.removeEventListener(MouseEvent.CLICK ,
onClick);
}
]]>
</mx:Script>
<mx:Button id="myButton"
initialize="myButton.addEventListener(MouseEvent.CLICK, onClick)" />
</mx:Application>
________________________________
From: [email protected]
[mailto:[EMAIL PROTECTED] On Behalf Of Firdosh Tangri
Sent: Monday, September 17, 2007 1:37 PM
To: [email protected]
Subject: Re: [flexcomponents] Removing Events defined by MXML
hey mike,
well yes your abs right but the thing is I am working on
a fairly large project right now
where a lot of events are defined in mxml so dont want to waste time on
finding all the events defined
in mxml and convert them to as.
cheers
firdosh
On 9/17/07, Michael Schmalle <[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> > wrote:
He's mixing apples and oranges.
It seems like he wants a one click handled event. Once it's clicked,
remove the listener. It seems to me Firdosh that the correct way to
implement the functionality of what you want is adding the event
listener in AS in a script tag. Using the hack you mentioned is not
necessary becasue you just need to add a couple lines of action script.
Why go to this trouble?
Peace, Mike
On 9/17/07, Gordon Smith < [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> wrote:
I'm trying to understand why you want to remove the event listener.
- Gordon
________________________________
From: [email protected]
<mailto:[email protected]> [mailto:flexcompone
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> ] On Behalf Of Firdosh
Tangri
Sent: Monday, September 17, 2007 7:14 AM
To: [email protected]
<mailto:[email protected]>
Subject: Re: [flexcomponents] Removing Events defined by MXML
thanks gordon but say if I dont remove the instance , but just want
remove the event listener
the removeEventListener would not work....cause the function would kept
be called
On 9/14/07, Gordon Smith <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> >
wrote:
> Therefore there will always be a reference to the object and GC would
not collect it.
I think you misunderstand. When you write
<mx:Button id="myButton" click="onClick"/>
you cause the Button instance (the dispatcher of the 'click' event) to
have a reference to the Application instance (the listener for the
'click' event) and not the other way around. A dispatcher keeps a list
of listeners, but listeners do not remember the dispatcher that they
registered with.
If you were to do
removeChild(myButton);
myButton = null;
the Button would be eligible for garbage-collection because the
Application would no longer have any reference to it.
But I doubt that you're dynamically removing components, so what kind of
leak were you worried about anyway?
- Gordon
________________________________
From: [email protected]
<mailto:[email protected]> [mailto:flexcompone
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> ] On Behalf Of Firdosh
Tangri
Sent: Friday, September 14, 2007 3:32 PM
To: [email protected]
<mailto:[email protected]>
Subject: Re: [flexcomponents] Removing Events defined by MXML
Well because the callback function keeps getting called even when I try
to remove the
event listener
<mx:Application>
<mx:Script>
<![CDATA[
private function onClick():void{
trace("click");
myButton.removeEventListener(MouseEvent.CLICK ,
onClick);
}
]]>
</mx:Script>
<mx:Button id="myButton" click="onClick" />
</mx:Application>
In the above example the onClick function keeps getting called and the
myButton.removeEventListener (MouseEvent.CLICK , onClick);
does not work.
Therefore there will always be a reference to the object and GC would
not collect it.
cheers
firdosh
On 9/14/07, Gordon Smith <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> >
wrote:
I just want to mention...
The name of the autogenerated event handler method for an MXML event
attribute is an undocumented implementation detail that is subject to
change without notice in future versions of Flex.
Also, I'm curious what you're trying to accomplish by removing these
listeners.
- Gordon
________________________________
From: [email protected]
<mailto:[email protected]> [mailto: flexcompone
<mailto:flexcompone> [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> ]
On Behalf Of Firdosh Tangri
Sent: Friday, September 14, 2007 9:20 AM
To: [email protected]
<mailto:[email protected]>
Subject: Re: [flexcomponents] Removing Events defined by MXML
Hey Josh,
thanks well I an working on a big project and a lot of
creation complete and other mxml defined events are there
I used a hack to kill the eventlistener
basically just did a
var comp:UIComponent = event.currentTarget as UIComponent;
comp.removeEventListener(MouseEvent.CLICK,
Application.application["__btn_click"];
so there is 2 underscore , the id of the component in this case "btn"
and then underscore again and then the event name , which is click here
and that basically took care of the problem.
cheers
firdosh
On 9/14/07, Josh Tynjala <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> >
wrote:
As far as I know, it's not possible to remove events of this type. The
Flex compiler actually generates a function to wrap around whatever you
call. You can see the exact code if you look at the generated code using
the -keep compiler argument, but it looks something like this:
private function someGeneratedFunctionName(event:MouseEvent):void
{
//your code gets copied here
onClick();
}
You can get an idea of why when you think of the fact that the "event"
parameter is available in your click="" attribute in the MXML. You can
either pass it to your function or ignore it completely. In
ActionScript, when you use addEventListener instead, your function must
always accept an event parameter.
Firdosh Tangri wrote:
hey all,
I was just wondering if i define an event by mxml like
<mx:Button id="myBtn" click="onClick();") />
and then try and remove it
private function onClick():void{
trace("Click");
myBtn.removeEventListener(MouseEvent.CLICK, onClick);
}
it still keeps calling the function but if i add the event listener
through as
like
myBtn.addEventListener (MouseEvent.CLICK , onClick);
then it removes the call back function.
thanks
cheers
firdosh
--
Teoti Graphix
http://www.teotigraphix.com <http://www.teotigraphix.com>
Blog - Flex2Components
http://www.flex2components.com <http://www.flex2components.com>
You can find more by solving the problem then by 'asking the question'.