首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

flex的event编程(兑现组件间传递数据)

2012-10-26 
flex的event编程(实现组件间传递数据)在任何的web应用开发中,在不同的组件中传递数据是一件非常重要的事情

flex的event编程(实现组件间传递数据)

在任何的web应用开发中,在不同的组件中传递数据是一件非常重要的事情,?flex的应用开 发也不例外,今天讲一下flex的event.

在flex中所有的UIComponent都持有flash.events.EventDispatcher对象,通过改对象我们能够让一个 application中的不同UIComponent广播自己的event.,让监听自己event的组件能够获得消息.当事件触发时执行响应的操作, 这样的一种事件驱动方式能够是代码很容易维护,和阅读.

事件的职能除了通知监听事件的组件还有就是传递数据.在事情触发的时候将数据传递给下一个组件,让下一个组件来完成接下去的工作是一个非常好的实 践.我们通过继承flash.events.Event对象来编写自己的event,并且让自己的event包含我们想要传递的数据.

例如ProductEvent他继承了flash.events.Event同时本身又持有product对象,这样使得下一个组件能够获得 product的所有信息,从而继续对product进行处理.

flex的event编程(兑现组件间传递数据)package?events
flex的event编程(兑现组件间传递数据)flex的event编程(兑现组件间传递数据){
flex的event编程(兑现组件间传递数据)????import?flash.events.Event;
flex的event编程(兑现组件间传递数据)????import?valueObjects.Product;
flex的event编程(兑现组件间传递数据)
flex的event编程(兑现组件间传递数据)????public?class?ProductEvent?extends?Event
flex的event编程(兑现组件间传递数据)flex的event编程(兑现组件间传递数据)????{
flex的event编程(兑现组件间传递数据)????????public?var?product:Product;
flex的event编程(兑现组件间传递数据)????????
flex的event编程(兑现组件间传递数据)flex的event编程(兑现组件间传递数据)????????public?function?ProductEvent(product:Product,type:String,bubbles:Boolean=false){
flex的event编程(兑现组件间传递数据)????????????super(type,bubbles);
flex的event编程(兑现组件间传递数据)????????????this.product=product;
flex的event编程(兑现组件间传递数据)????????}
flex的event编程(兑现组件间传递数据)????????
flex的event编程(兑现组件间传递数据)flex的event编程(兑现组件间传递数据)????????public?override?function?clone():Event{
flex的event编程(兑现组件间传递数据)????????????return?new?ProductEvent(product,type,bubbles);
flex的event编程(兑现组件间传递数据)????????}
flex的event编程(兑现组件间传递数据)????}
flex的event编程(兑现组件间传递数据)}

有了ProductEvent对象我们现在要做的就是如何使用这个对象使他在不同的组件中传递.在下面的示例中当我们点击add Botton的时候就会调用itemAdded()方法,改方法创建了一个ProductEvent实例,并且通过调用 this.dispatchEvent(e)方法来广播这个event,同时注意在这个mxml的顶部声明了自己所持有的event是何种类型,还有 event的名字,这个名字将被别的组件用来判断是何种event被触发.

flex的event编程(兑现组件间传递数据)<?xml?version="1.0"?encoding="utf-8"?>
flex的event编程(兑现组件间传递数据)<mx:Canvas?xmlns:mx="http://www.adobe.com/2006/mxml"?
flex的event编程(兑现组件间传递数据)????horizontalScrollPolicy="off"
flex的event编程(兑现组件间传递数据)??verticalScrollPolicy="off">
flex的event编程(兑现组件间传递数据)??<mx:Metadata>
flex的event编程(兑现组件间传递数据)??????[Event(name="itemAdded",type="events.ProductEvent")]
flex的event编程(兑现组件间传递数据)??</mx:Metadata>
flex的event编程(兑现组件间传递数据)??
flex的event编程(兑现组件间传递数据)????<mx:Script>
flex的event编程(兑现组件间传递数据)????????<![CDATA[
flex的event编程(兑现组件间传递数据)????????????import?events.ProductEvent;
flex的event编程(兑现组件间传递数据)????????????import?mx.core.Application;
flex的event编程(兑现组件间传递数据)????????????import?valueObjects.Product;
flex的event编程(兑现组件间传递数据)????????????[Bindable]
flex的event编程(兑现组件间传递数据)????????????public?var?groceryItem:Product;
flex的event编程(兑现组件间传递数据)????????????
flex的event编程(兑现组件间传递数据)flex的event编程(兑现组件间传递数据)????????????private?function?itemAdded(prod:Product):void{
flex的event编程(兑现组件间传递数据)??????????var?e:ProductEvent?=?new?ProductEvent(prod,"itemAdded",true);
flex的event编程(兑现组件间传递数据)???????????????????this.dispatchEvent(e);
flex的event编程(兑现组件间传递数据)??????}
flex的event编程(兑现组件间传递数据)??????
flex的event编程(兑现组件间传递数据)flex的event编程(兑现组件间传递数据)??????private?function?toggleState(state:String):void{
flex的event编程(兑现组件间传递数据)flex的event编程(兑现组件间传递数据)???????????????if(state?==?"closed"){
flex的event编程(兑现组件间传递数据)??????????????????this.currentState?=?"";
flex的event编程(兑现组件间传递数据)flex的event编程(兑现组件间传递数据)???????????????}?else?{
flex的event编程(兑现组件间传递数据)?????????????????this.currentState?=?"expanded";
flex的event编程(兑现组件间传递数据)???????????????}
flex的event编程(兑现组件间传递数据)????????????}
flex的event编程(兑现组件间传递数据)
flex的event编程(兑现组件间传递数据)
flex的event编程(兑现组件间传递数据)????????]]>
flex的event编程(兑现组件间传递数据)????</mx:Script>
flex的event编程(兑现组件间传递数据)????
flex的event编程(兑现组件间传递数据)????<mx:states>
flex的event编程(兑现组件间传递数据)???<mx:State?name="expanded">
flex的event编程(兑现组件间传递数据)??????<mx:SetProperty?target="{add}"?name="visible"?value="true"/>
flex的event编程(兑现组件间传递数据)??????<mx:AddChild>
flex的event编程(兑现组件间传递数据)?????????<mx:VBox?width="100%"?x="200">
flex的event编程(兑现组件间传递数据)????????????<mx:Text?text="{groceryItem.description}"?width="50%"/>
flex的event编程(兑现组件间传递数据)????????????????????<mx:Label?text="Low?Fat"
flex的event编程(兑现组件间传递数据)??????????????????x="100"?y="60"
flex的event编程(兑现组件间传递数据)??????????????????visible="{groceryItem.isLowFat}"/>
flex的event编程(兑现组件间传递数据)?????????</mx:VBox>
flex的event编程(兑现组件间传递数据)??????</mx:AddChild>
flex的event编程(兑现组件间传递数据)???</mx:State>
flex的event编程(兑现组件间传递数据)</mx:states>
flex的event编程(兑现组件间传递数据)????
flex的event编程(兑现组件间传递数据)????<mx:Canvas?mouseOver="toggleState('open')"?mouseOut="toggleState('closed')">
flex的event编程(兑现组件间传递数据)????????<mx:Image?id="pic"
flex的event编程(兑现组件间传递数据)???????source="{'../assets/'+groceryItem.imageName}"/>
flex的event编程(兑现组件间传递数据)????
flex的event编程(兑现组件间传递数据)????????<mx:Label?id="price"
flex的event编程(兑现组件间传递数据)???????text="{groceryItem.listPrice}"
flex的event编程(兑现组件间传递数据)???????x="100"?y="20"/>
flex的event编程(兑现组件间传递数据)????
flex的event编程(兑现组件间传递数据)????????<mx:Button?id="add"
flex的event编程(兑现组件间传递数据)?????????visible="false"
flex的event编程(兑现组件间传递数据)???????label="Add?To?Cart"
flex的event编程(兑现组件间传递数据)???????click="itemAdded(groceryItem)"
flex的event编程(兑现组件间传递数据)???????x="100"?y="40"/>
flex的event编程(兑现组件间传递数据)???</mx:Canvas>
flex的event编程(兑现组件间传递数据)
flex的event编程(兑现组件间传递数据)</mx:Canvas>
flex的event编程(兑现组件间传递数据)

现在我们看一下上面这个mxml的父组件是如何监听的,当itemAdded事件触发时就会调用logItemAdded()方法.

同时这个mxml自己也定义了itemAdded事件,可以将这个事件作为自己的事件继续广播.

flex的event编程(兑现组件间传递数据)<?xml?version="1.0"?encoding="utf-8"?>
flex的event编程(兑现组件间传递数据)<mx:VBox?xmlns:mx="http://www.adobe.com/2006/mxml"?xmlns:v="views.ecomm.*">
flex的event编程(兑现组件间传递数据)<mx:Metadata>
flex的event编程(兑现组件间传递数据)???[Event(name="itemAdded",type="events.ProductEvent")]
flex的event编程(兑现组件间传递数据)</mx:Metadata>
flex的event编程(兑现组件间传递数据)
flex的event编程(兑现组件间传递数据)????<mx:Script>
flex的event编程(兑现组件间传递数据)????????<![CDATA[
flex的event编程(兑现组件间传递数据)????????????import?events.ProductEvent;
flex的event编程(兑现组件间传递数据)????????????import?mx.collections.ArrayCollection;
flex的event编程(兑现组件间传递数据)????????????[Bindable]
flex的event编程(兑现组件间传递数据)????????????public?var?prodByCategory:ArrayCollection;
flex的event编程(兑现组件间传递数据)????????????
flex的event编程(兑现组件间传递数据)flex的event编程(兑现组件间传递数据)????????????private?function?logItemAdded(event:ProductEvent):void{
flex的event编程(兑现组件间传递数据)????????????????trace(event.product);
flex的event编程(兑现组件间传递数据)????????????????this.dispatchEvent(event);
flex的event编程(兑现组件间传递数据)????????????}
flex的event编程(兑现组件间传递数据)????????]]>
flex的event编程(兑现组件间传递数据)????</mx:Script>
flex的event编程(兑现组件间传递数据)????<mx:Repeater?id="foodRepeater"
flex的event编程(兑现组件间传递数据)???????width="100%"?height="100%"
flex的event编程(兑现组件间传递数据)???????dataProvider="{prodByCategory}">
flex的event编程(兑现组件间传递数据)???????<v:GroceryDetail?id="prod"?
flex的event编程(兑现组件间传递数据)?????????width="80%"

flex的event编程(兑现组件间传递数据)?????????groceryItem="{foodRepeater.currentItem}"?itemAdded="logItemAdded(event)"/>
flex的event编程(兑现组件间传递数据)????</mx:Repeater>
flex的event编程(兑现组件间传递数据)</mx:VBox>
flex的event编程(兑现组件间传递数据)

但是如果一个application中有非常深的组件层次如果最底层的event事件被最上层的组件所调用,按照这种方法,这个两个组件中间的组件 中每次都写这样的方法来传递event是非常麻烦,并且恶心的,还好flex对event提供了一个bubbles属性(冒泡),这个属性默认为 false,当显示的设置为true我们就可以在中间的那些组件中不用编写itemAdded="logItemAdded(event)类似的代码,这 样可以节省很多的编码工作,也减少的出错概率,但是中间的那些组件必须在<Metadata>节点中声明需要传递的这个event.例如上面 的示例可以简单的写为

flex的event编程(兑现组件间传递数据)<?xml?version="1.0"?encoding="utf-8"?>
flex的event编程(兑现组件间传递数据)<mx:VBox?xmlns:mx="http://www.adobe.com/2006/mxml"?xmlns:v="views.ecomm.*">
flex的event编程(兑现组件间传递数据)<mx:Metadata>
flex的event编程(兑现组件间传递数据)???[Event(name="itemAdded",type="events.ProductEvent")]
flex的event编程(兑现组件间传递数据)</mx:Metadata>
flex的event编程(兑现组件间传递数据)
flex的event编程(兑现组件间传递数据)????<mx:Script>
flex的event编程(兑现组件间传递数据)????????<![CDATA[
flex的event编程(兑现组件间传递数据)????????????import?events.ProductEvent;
flex的event编程(兑现组件间传递数据)????????????import?mx.collections.ArrayCollection;
flex的event编程(兑现组件间传递数据)????????????[Bindable]
flex的event编程(兑现组件间传递数据)????????????public?var?prodByCategory:ArrayCollection;
flex的event编程(兑现组件间传递数据)
flex的event编程(兑现组件间传递数据)????????]]>
flex的event编程(兑现组件间传递数据)????</mx:Script>
flex的event编程(兑现组件间传递数据)????<mx:Repeater?id="foodRepeater"
flex的event编程(兑现组件间传递数据)???????width="100%"?height="100%"
flex的event编程(兑现组件间传递数据)???????dataProvider="{prodByCategory}">
flex的event编程(兑现组件间传递数据)???????<v:GroceryDetail?id="prod"?
flex的event编程(兑现组件间传递数据)?????????width="80%"

flex的event编程(兑现组件间传递数据)?????????groceryItem="{foodRepeater.currentItem}"/>
flex的event编程(兑现组件间传递数据)????</mx:Repeater>
flex的event编程(兑现组件间传递数据)</mx:VBox>

热点排行