将组件变量值获取到主应用程序中

时间:2012-03-27 10:20:35

标签: flex components flash-builder flex4.5

我有一个组件可以获取单选按钮

选择的数据
<mx:itemRenderer >
                            <fx:Component id="radio">
                            <mx:RadioButton selected="false" useHandCursor="true" change="item_changeHandler(event)">
                                    <fx:Script>
                                    <![CDATA[
                                            private var data_id:int;
                                            private var data_name:String;
                                            protected function item_changeHandler(event:Event):void
                                            {
                                                data_id=data.id;
                                                data_name=data.name;
                                            }
                                    ]]>
                                    </fx:Script>
                            </mx:RadioButton>
                            </fx:Component>
                        </mx:itemRenderer>

这里有2个变量data_id和data_name,我想在主应用程序中使用这个组件变量,当我点击一个按钮时会调用它,那么怎么做我如何将变量值调用到这个函数中我试过这个

protected function delete_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                dispatchEvent( new Event( Event.CHANGE ) );

                deleteTaxonomy(data.id, data.name);
            }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,那么当 id name 更改并在主应用程序中捕获它时,我会调度一个事件。例如。您可以使用变量id和name创建一个CustomEvent类 YouDataEvent.as 。我经常使用自己的对象,然后使用事件发送对象。让我们调用包含id和名称​​ YourObject

的对象
import flash.events.Event;

public class YourObjectEvent extends Event
{
         public static const YOURDATA_UPDATED:String = "yourdataUpdated";
         public var yourObj:YourObject

         public function YourObjectEvent(type:String, yourObjInput:YourObject, bubbles:Boolean = true, cancelable:Boolean = false)
         {
            this.yourObj = yourObjInput;
            super(type, bubbles, cancelable);
         }

}

然后您可以在数据更改时调度事件,例如在* item_changeHandler *方法中如下:

dispatchEvent(new YourObjectEvent(YourObjectEvent.YOURDATA_UPDATED, yourObjVar));   

并在主应用程序中捕获它,例如:

FlexGlobals.topLevelApplication.addEventListener(YourObjectEvent.YOURDATA_UPDATED, yourObjUpdatedHandler);

private function yourObjUpdatedHandler(event:YourObjectEvent): void {
  //DO something
  var id:Number = event.yourObj.id;
  var name:String = event.yourObj.name
}

希望这有帮助!