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

问题描述:

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

编辑:在这里添加整个东西。

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

你的代码没有中继器;你确定你发布了正确的代码段去解决这个问题吗? – JeffryHouser 2011-05-04 12:34:34

+0

固定代码也包含主应用程序。 – 2011-05-04 13:04:37

+0

再次尝试使用可运行的样本。现在你只是有一些不相干的东西。 – JeffryHouser 2011-05-04 13:12:12

哦,亲爱的,什么样的Flex 3和Flex 4的混搭。如果你打算使用Flex 4,使用所有Spark组件。 VBox,Canvas,Repeater都有Spark相当于; VGroup,Group和DataGroup。

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

当前遇到的问题是Repeater本身的问题,因为中继器的任何“子”都在它自己的范围内,绑定到中继器外部的任何内容都将默默失败。这可以通过使用DataGroup并在事件输出概念中使用正确的数据来避免。

+0

感谢您的留言。我会尝试熟悉DataGroup等新的火花组件。 – 2011-05-10 19:32:16

+0

得到解决。 ItemRenderers不能将事件分派给应用程序,所以我不得不将eventlistener添加到渲染父级。 – 2011-06-13 17:48:33