从转发器组件调度自定义事件并在其他转发器组件中监听事件

时间:2011-05-04 09:07:29

标签: flex events actionscript repeater

我有一个带有自定义事件的转发器组件。我的转发器组件触发自定义事件保存数据。我需要调度事件,以便其他转发器组件可以从调度组件访问数据。需要帮助吗?

编辑:在这里添加了全部内容。

    <!-- AttributeMapping.mxml -->
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="40"
     creationComplete="creationCompleteHandler(event)">

<fx:Metadata>
    [Event(name="mappingChanged", type="MappingChangeEvent")]
</fx:Metadata>

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.collections.ArrayList;
        import mx.events.FlexEvent;
        import mx.events.ValidationResultEvent;
        import mx.validators.ValidationResult;

        import spark.events.IndexChangeEvent;

        private var mappedAttribute:String;

        [Bindable]
        private var _sourceAttribute:String;

        [Bindable]
        private var targetAttributeCollection:ArrayCollection = new ArrayCollection();

        private var _targetAttributes:Array;

        private var _targetAttribute:String;

        public function get targetAttribute():String
        {
            return _targetAttribute;
        }

        public function get targetAttributes():Array
        {
            return _targetAttributes;
        }

        public function set targetAttributes(value:Array):void
        {
            _targetAttributes = value;
            targetAttributeCollection.source = _targetAttributes;
        }

        public function get sourceAttribute():String
        {
            return _sourceAttribute;
        }

        public function set sourceAttribute(value:String):void
        {
            _sourceAttribute = value;
        }

        protected function creationCompleteHandler(event:FlexEvent):void
        {
            this.addEventListener(MappingChangeEvent.MAPPING_CHANGED, mappingsChanged);
        }

        protected function mappingsChanged(event:MappingChangeEvent):void
        {
            lblFuck.text = event.MappedAttribute();
        }

        protected function cmbTargetAttribute_changeHandler(event:IndexChangeEvent):void
        {
            this._targetAttribute = cmbTargetAttribute.selectedItem as String;
            dispatchEvent(new MappingChangeEvent(MappingChangeEvent.MAPPING_CHANGED, true, _targetAttribute));  
        }

    ]]>
</fx:Script>
<s:Label text="{_sourceAttribute}" x="10" verticalCenter="0" id="lblSourceAttribute"/>
<!-- <s:DropDownList y="9" right="10" id="ddlTargetAttribute" dataProvider="{targetAttributeCollection}" width="217" prompt="Select target attribute..."/> -->
<s:ComboBox  id="cmbTargetAttribute" change="cmbTargetAttribute_changeHandler(event)" dataProvider="{targetAttributeCollection}" right="10" verticalCenter="0"/>

<!-- Main.mxml -->
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           minWidth="955" minHeight="600" xmlns:controls="*">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.ValidationResultEvent;
        import mx.validators.ValidationResult;

        [Bindable]
        private var targetAttributes:Array = new Array("Product Name", "Price", "Weight", "Lot Size", "Currency", "VAT", "Maximum Order Quantity");

        [Bindable]
        private var sourceAttributes:Array = new Array("Name", "Price", "Product Weight", "Size", "Currency", "VAT", "Max Order Quantity");

    ]]>
</fx:Script>
<mx:VBox width="492" top="10" bottom="10" left="28">
    <mx:Repeater id="attributeMap" dataProvider="{sourceAttributes}">
        <controls:AttributeMapping targetAttributes="{targetAttributes}" sourceAttribute="{attributeMap.currentItem}"/>
    </mx:Repeater>
</mx:VBox>

<!-- MappingChangeEvent.as -->
package
{
    import flash.events.Event;

    public class MappingChangeEvent extends Event
    {
    public static const MAPPING_CHANGED:String = "mappingChanged";

    private var attribute:String;

    public function MappingChangeEvent(type:String, bubbles:Boolean, attribute:String)
    {
        super(type, bubbles);
        this.attribute = attribute;
    }

    override public function clone():Event
    {
        return new MappingChangeEvent(type, bubbles, attribute);
    }

    public function MappedAttribute():String
    {
        return this.attribute;
    }
    }
}

1 个答案:

答案 0 :(得分:1)

哦,亲爱的,什么是Flex 3和Flex 4的混搭。如果您打算使用Flex 4,请使用所有Spark组件。 VBox,Canvas,Repeater都有Spark等价物;分别为VGroup,Group和DataGroup。

我建议您使用DataGroup而不是Repeater,因为已知转发器存在问题,而且DataGroup更易于管理。只需添加一个自定义项目渲染器即可完成任何操作。

您遇到的当前问题是Repeater本身存在问题,因为转发器的任何“子”都在其自身的范围内,并且任何绑定到转发器外部的内容都将无声地失败。通过使用DataGroup并在事件输出概念中使用适当的数据可以避免这种情况。