Where do I put custom code in a custom component in Flash Builder?
in the main file, I would write:
<components:mybutton id="mybutton1" rollOver="point_rollOverHandler(event)" />
But if I want the component to have that behaviour innately, where do 开发者_如何学GoI write it in the mybutton mxml file to have it reference itself?
I tried <s:rollOver="point_rollOverHandler(event)"/>
but it complains the element type must be followed by either atrribute specifications, > or />
My guess is that your component is based on s:Button. Just declare the rollOver method in your components main MXML node like this (line 5):
// myButton.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
rollOver="button1_rollOverHandler(event)"
>
<fx:Script>
<![CDATA[
protected function button1_rollOverHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
</s:Button>
in the root tag for your custom component add the property :
creationComplete="init()"
then in the script tag of your custom component create that function and set up the mouse event listener:
function init():void{
this.addEventListener(MouseEvent.MOUSE_OVER, point_rollOverHandler)
}
but this will call the function point_rollOverHandler(e:MouseEvent) defined in your custom component. If you're looking to call a function defined on it's parent then what you have is the best way. Otherwise you'd be tying the components too tightly together which would make your code brittle and less reusable.
精彩评论