从中继器组件调度自定义事件,并在其他中继器组件中侦听事件



我有一个自定义事件的重复器组件。我的中继器组件触发自定义事件保存数据。我的需要是调度事件,以便其他中继器组件可以从调度组件访问数据。需要帮忙吗?

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

    <!-- 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;
    }
    }
}

天哪,这是Flex 3和Flex 4的混搭。如果您打算使用Flex 4,请使用所有Spark组件。VBox, Canvas, Repeater都具有Spark等效;VGroup, Group, dataggroup .

我建议您使用dataggroup而不是Repeater,因为中继器已经知道有问题,加上dataggroup更容易管理。只需要添加一个自定义项目渲染器来做任何你想做的事情。

当前的问题,你正在经历的是一个问题与中继器本身,因为任何中继器的"子"是在它自己的范围内,任何绑定到中继器外的东西将无声地失败。这可以通过使用dataggroup和使用适当的data in, event out概念来避免。

最新更新